2

以下是我检测蓝牙相关操作的代码。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.i(TAG, "-------------------Bluetooth Operation-----------------:");
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            if (state == BluetoothAdapter.STATE_ON) {
                Log.i(TAG, "-------------------BlueTooth ON-----------------");
            }else if(state == BluetoothAdapter.STATE_OFF){
                Log.i(TAG, "-------------------BlueTooth OFF-----------------");

            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.i(TAG, "-------------------BlueTooth Discover Started-----------------");
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Log.i(TAG, "-------------------BlueTooth Discover Finished-----------------");

        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i(TAG, "-------------------New Device Found-----------------");
            Log.i(TAG, "-------------------------NAME        :" + device.getName());
            Log.i(TAG, "-------------------------MAC ADDRESS :" + device.getAddress());
        }
    }
};

它适用于大多数 Android 设备。但问题仅适用于带有 Marshmallow 的 Android 设备。当找到新设备时,它们不会返回BluetoothDevice.ACTION_FOUND 。我已按照开发人员指南中的说明正确注册了广播接收器

任何解决方案...?

4

0 回答 0