[Android Pro] 监听Blutooth打开广播

   <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        IntentFilter filter = new IntentFilter();
        filter.setPriority(2147483647);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothReceiver, filter);

 

private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                    switch (bluetoothState) {
                        case BluetoothAdapter.STATE_OFF :
                            if (FeatureConfig.DEBUG_LOG) {
                                Log.e("H3c", "bluetoothState STATE_OFF");
                            }
                            break;
                        case BluetoothAdapter.STATE_ON :
                            if (FeatureConfig.DEBUG_LOG) {
                                Log.e("H3c", "bluetoothState STATE_ON ");
                            }
                            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
              .getDefaultAdapter();
                
boolean result = false;
                 if (mBluetoothAdapter != null) {
                 result = mBluetoothAdapter.disable();
                 }
break; } } } } };

open bluetooth

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
       .getDefaultAdapter();
 if (mBluetoothAdapter != null) {
       mBluetoothAdapter.enable();
 }

 

posted @ 2015-09-23 10:36  demoblog  阅读(363)  评论(0编辑  收藏  举报