一些与蓝牙有关的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 headset to 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) {
            // }
        }
    }
}
 

 

 

 

.

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值