Java Code Examples for android.bluetooth.BluetoothAdapter#ACTION_CONNECTION_STATE_CHANGED

The following examples show how to use android.bluetooth.BluetoothAdapter#ACTION_CONNECTION_STATE_CHANGED . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: AssistantBySDK   File: AssistantService.java    License: Apache License 2.0 5 votes vote down vote up
public void registerReveicer() {
    registerReceiver(netWorkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    IntentFilter inf = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    inf.addAction(Intent.ACTION_SCREEN_ON);
    registerReceiver(scrennReceiver, inf);
    registerReceiver(headSetInReceiver, new IntentFilter("android.intent.action.HEADSET_PLUG"));
    IntentFilter bit = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    // bit.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    //bit.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
    registerReceiver(blueConnectStateBroadcastReceiver, bit);
    IntentFilter smsFilter = new IntentFilter(SMS_RECEIVED_ACTION);
    smsFilter.setPriority(2147483647);
    smsFilter.addAction(GSM_SMS_RECEIVED_ACTION);
    registerReceiver(smsReceiver, smsFilter);
}
 
Example 2
Source Project: scangon   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void registerBoradcastReceiver() {
    IntentFilter filter1 = new IntentFilter(
            BluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter filter2 = new IntentFilter(
            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    registerReceiver(stateChangeReceiver, filter1);
    registerReceiver(stateChangeReceiver, filter2);
}
 
Example 3
private void setupBluetoothBroadcastReceiver(){
    String[] actions = new String[4];
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        actions[0] = BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED;
    }else{
        actions[0] = "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED";
    }
    actions[1] = BluetoothAdapter.ACTION_STATE_CHANGED;
    actions[2] = BluetoothDevice.ACTION_ACL_DISCONNECTED;
    actions[3] = BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED;

    listenForIntents(actions);
}
 
Example 4
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    BluetoothDevice dev;
    int state;
    if (action == null) {
        return;
    }
    switch (action) {
        /**
         * 蓝牙开关状态
         * int STATE_OFF = 10; //蓝牙关闭
         * int STATE_ON = 12; //蓝牙打开
         * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
         * int STATE_TURNING_ON = 11; //蓝牙正在打开
         */
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
            mCallback.onStateSwitch(state);
            break;
        /**
         * 蓝牙开始搜索
         */
        case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
            CbtLogs.i("蓝牙开始搜索");
            break;
        /**
         * 蓝牙搜索结束
         */
        case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
            CbtLogs.i("蓝牙扫描结束");
            mCallback.onScanStop();
            break;
        /**
         * 发现新设备
         */
        case BluetoothDevice.ACTION_FOUND:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mCallback.onFindDevice(dev);
            break;
        /**
         * 设备配对状态改变
         * int BOND_NONE = 10; //配对没有成功
         * int BOND_BONDING = 11; //配对中
         * int BOND_BONDED = 12; //配对成功
         */
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("设备配对状态改变:" + dev.getBondState());
            break;
        /**
         * 设备建立连接
         * int STATE_DISCONNECTED = 0; //未连接
         * int STATE_CONNECTING = 1; //连接中
         * int STATE_CONNECTED = 2; //连接成功
         */
        case BluetoothDevice.ACTION_ACL_CONNECTED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("设备建立连接:" + dev.getBondState());
            mCallback.onConnect(dev);
            break;
        /**
         * 设备断开连接
         */
        case BluetoothDevice.ACTION_ACL_DISCONNECTED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // mCallback.onConnect(dev.getBondState(), dev);
            break;
        /**
         * 本地蓝牙适配器
         * BluetoothAdapter连接状态
         */
        case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        /**
         * 提供用于手机的蓝牙耳机支持
         * BluetoothHeadset连接状态
         */
        case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        /**
         * 定义高质量音频可以从一个设备通过蓝牙连接传输到另一个设备
         * BluetoothA2dp连接状态
         */
        case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        default:
            break;
    }
}