首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

一些与蓝牙相关的code

2012-10-05 
一些与蓝牙有关的code注册RecevierIntentFilter audioStateFilter new IntentFilter()audioStateFilter

一些与蓝牙有关的code

注册Recevier

IntentFilter audioStateFilter = new IntentFilter();audioStateFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);audioStateFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);mReceiver = new VoiceDialerBroadcastReceiver();registerReceiver(mReceiver, audioStateFilter);

取消Receiver

unregisterReceiver(mReceiver);

Receiver的实现

?

private class VoiceDialerBroadcastReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);            int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);            if (false) Log.d(TAG, "HEADSET STATE -> " + state);            if (state == BluetoothProfile.STATE_CONNECTED) {                if (device == null) {                    return;                }                mBluetoothDevice = device;                updateBluetoothParameters(true);            } else if (state == BluetoothProfile.STATE_DISCONNECTED) {                mBluetoothDevice = null;                updateBluetoothParameters(false);            }        } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {            int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);            int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);            if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED &&                mWaitingForScoConnection) {                // SCO channel has just become available.                mWaitingForScoConnection = false;                if (mWaitingForTts) {                    // still waiting for the TTS to be set up.                } else {                    // we now have SCO connection and TTS, so we can start.                    mHandler.postDelayed(new GreetingRunnable(), FIRST_UTTERANCE_DELAY);                }            } else if (prevState == BluetoothHeadset.STATE_AUDIO_CONNECTED) {                if (!mWaitingForScoConnection) {                    // apparently our connection to the headset has dropped.                    // we won't be able to continue voicedialing.                    if (false) Log.d(TAG, "lost sco connection");                    mHandler.post(new ErrorRunnable(                            R.string.headset_connection_lost));                    exitActivity();                }            }        }    }}

蓝牙语音识别

mBluetoothHeadset.startVoiceRecognition(mBluetoothDevice);mBluetoothHeadset.stopVoiceRecognition(mBluetoothDevice);

?

mAdapter = BluetoothAdapter.getDefaultAdapter();if (BluetoothHeadset.isBluetoothVoiceDialingEnabled(this) && mAdapter != null) {    if (!mAdapter.getProfileProxy(this, mBluetoothHeadsetServiceListener,        BluetoothProfile.HEADSET)) {            Log.e(TAG, "Getting Headset Proxy failed");    }}mAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);

?

private BluetoothProfile.ServiceListener mBluetoothHeadsetServiceListener =        new BluetoothProfile.ServiceListener() {    public void onServiceConnected(int profile, BluetoothProfile proxy) {        if (false) Log.d(TAG, "onServiceConnected");        mBluetoothHeadset = (BluetoothHeadset) proxy;        List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();        if (deviceList.size() > 0) {            mBluetoothDevice = deviceList.get(0);            int state = mBluetoothHeadset.getConnectionState(mBluetoothDevice);            if (false) Log.d(TAG, "headset status " + state);            // We are already connnected to a headset            if (state == BluetoothHeadset.STATE_CONNECTED) {                updateBluetoothParameters(true);                return;            }        }        updateBluetoothParameters(false);    }    public void onServiceDisconnected(int profile) {        mBluetoothHeadset = null;    }};
?

?

Detect the connection status of a paired Bluetooth headsetto the phone.In Android 3.0 (API level 11) "BluetoothHeadset" class has"isAudioConnected()" method.

adapter = BluetoothAdapter.getDefaultAdapter();adapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);private BluetoothProfile.ServiceListener mProfileListener =            new BluetoothProfile.ServiceListener() {        public void onServiceConnected(int profile, BluetoothProfile proxy) {            if (profile == BluetoothProfile.HEADSET) {                mBluetoothHeadset = (BluetoothHeadset) proxy;            }        }        public void onServiceDisconnected(int profile) {            if (profile == BluetoothProfile.HEADSET) {                mBluetoothHeadset = null;            }       }    };
?

Use mBluetoothHeadset to call the functions on BluetoothHeadset.java

Note: Audio Connection is different from Connection.

BluetoothProfile.ServiceListener b = new BlueToothListener();            boolean profileProxy = BluetoothAdapter.getDefaultAdapter()                    .getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET);public class BlueToothListener implements ServiceListener {        public static BluetoothHeadset headset;        public static BluetoothDevice bluetoothDevice;    @Override    public void onServiceDisconnected(int profile) {// dont care        headset = null;    }    @Override    public void onServiceConnected(int profile,            BluetoothProfile proxy) {// dont care        try {            Debugger.test("BluetoothProfile onServiceConnected "+proxy);            if (proxy instanceof BluetoothHeadset)                headset = ((BluetoothHeadset) proxy);            else// getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET);                 return;// ^^ => NEVER            List<BluetoothDevice> connectedDevices = proxy                    .getConnectedDevices();            for (BluetoothDevice device : connectedDevices) {                Debugger.log("BluetoothDevice found :" + device);                bluetoothDevice = device;                int connectionState = headset.getConnectionState(bluetoothDevice);                Debugger.log("BluetoothHeadset connectionState "+connectionState);//2 == OK                boolean startVoiceRecognition = headset                        .startVoiceRecognition(device);                if (startVoiceRecognition) {                    Debugger                            .log("BluetoothHeadset init Listener OK");                    return;                }                else                     Notify.popup("Bluetooth headset can't start speech recognition");            }        } catch (Exception e) {            // }        }    }}
?

?

?

?

.

?

?

?

?

?

?

?

?

?

?

?

热点排行