Android蓝牙无法发现附近的设备

时间:2016-05-20 17:09:47

标签: android bluetooth android-bluetooth

我正在制作一个需要与蓝牙设备连接的Android应用程序。我按照Android开发人员的文档,但似乎bluetoothReceiver中的onReceive()函数永远不会被调用。没有找到任何设备。

权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

主要活动的onCreate()

bluetoothConnect.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        enableBluetooth();
    }
});

enableBluetooth() - 用于启动蓝牙

private void enableBluetooth() {
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    //Bluetooth not supported on device
    if(bluetoothAdapter == null){
        Toast.makeText(getApplicationContext(), "Device Not Supporting Bluetooth", Toast.LENGTH_SHORT).show();
    }

    //Bluetooth not enabled
    if(!bluetoothAdapter.isEnabled()){
        //Open setting to enable bluetooth
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
    }
}

discoverBluetooth() - 添加到列表中发现的蓝牙设备并打印到logcat

  private void discoverBluetooth() {
         bluetoothReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.d("Device action: ", action);
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    String discoveredDeviceName = device.getName() + " " + device.getAddress();
                    bluetoothList.add(discoveredDeviceName);

                    Log.d("Device: ", discoveredDeviceName);
                }
            }
        };

    }

onActivityResult() - 如果用户启动蓝牙调用startDiscovery()并调用上面的函数discoverBluetooth()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_ENABLE_BT){
        if(resultCode == RESULT_OK){
            Toast.makeText(getApplicationContext(), "Bluetooth Connected" , Toast.LENGTH_SHORT).show();
            // Register the BroadcastReceiver

            discoverBluetooth();
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(bluetoothReceiver, filter); // Don't forget to unregister during onDestroy
            boolean isStarted = bluetoothAdapter.startDiscovery();
            Toast.makeText(getApplicationContext(), "Bluetooth Discovery status:" + (isStarted? " Started" : " Not Started"), Toast.LENGTH_SHORT).show();


        }else{
            Toast.makeText(getApplicationContext(), "Bluetooth Not Connected" , Toast.LENGTH_SHORT).show();
        }
    }
}

的onStop()

@Override
protected void onDestroy() {
    unregisterReceiver(bluetoothReceiver);
    super.onDestroy();

}

1 个答案:

答案 0 :(得分:1)

我认为您应该在AndroidManifest.xml中添加蓝牙相关权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>