How to turn off bluetooth device and sound device in Cocoa?

1.1k Views Asked by At

I've known that Airport can be turned off by CoreWLAN framework.

So, I think there are probably functions or frameworks related with bluetooth device and sound device.

How can I turn off that devices?

1

There are 1 best solutions below

0
On

I assume you by "cannot have power so that it cannot speak", you mean you simply want to mute the speaker. I found some neat sample code here, using CoreAudio to mute the system's default speaker: http://cocoadev.com/index.pl?SoundVolume

I took the liberty of converting it to pure C and trying it out.

#import <CoreAudio/CoreAudio.h>
#import <stdio.h>

// getting system volume

float getVolume() {
    float           b_vol;
    OSStatus        err;
    AudioDeviceID   device;
    UInt32          size;
    UInt32          channels[2];
    float           volume[2];

    // get device
    size = sizeof device;
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err!=noErr) {
        printf("audio-volume error get device\n");
        return 0.0;
    }

    // try set master volume (channel 0)
    size = sizeof b_vol;
    err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyVolumeScalar, &size, &b_vol);    //kAudioDevicePropertyVolumeScalarToDecibels
    if(noErr==err) return b_vol;

    // otherwise, try seperate channels
    // get channel numbers
    size = sizeof(channels);
    err = AudioDeviceGetProperty(device, 0, 0,kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
    if(err!=noErr) printf("error getting channel-numbers\n");

        size = sizeof(float);
        err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[0]);
        if(noErr!=err) printf("error getting volume of channel %d\n",channels[0]);
            err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[1]);
            if(noErr!=err) printf("error getting volume of channel %d\n",channels[1]);

                b_vol = (volume[0]+volume[1])/2.00;

                return  b_vol;
}


// setting system volume
void setVolume(float involume) {
    OSStatus        err;
    AudioDeviceID       device;
    UInt32          size;
    Boolean         canset  = false;
    UInt32          channels[2];
    //float         volume[2];

    // get default device
    size = sizeof device;
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err!=noErr) {
        printf("audio-volume error get device\n");
        return;
    }


    // try set master-channel (0) volume
    size = sizeof canset;
    err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &size, &canset);
    if(err==noErr && canset==true) {
        size = sizeof involume;
        err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &involume);
        return;
    }

    // else, try seperate channes
    // get channels
    size = sizeof(channels);
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
    if(err!=noErr) {
        printf("error getting channel-numbers\n");
        return;
    }

    // set volume
    size = sizeof(float);
    err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &involume);
    if(noErr!=err) printf("error setting volume of channel %d\n",channels[0]);
        err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &involume);
        if(noErr!=err) printf("error setting volume of channel %d\n",channels[1]);

}




int main() {
    printf("The system's volume is currently %f\n", getVolume());
    printf("Setting volume to 0.\n");
    setVolume(0.0f);
    return 0;
}

I ran it and got this:

[04:29:03] [william@enterprise ~/Documents/Programming/c]$ gcc -framework CoreAudio -o mute.o coreaudio.c 
.. snipped compiler output..
[04:29:26] [william@enterprise ~/Documents/Programming/c]$ ./mute.o 
The system's volume is currently 0.436749
Setting volume to 0.

Hopefully this sends you in the right direction.