BluetoothDevice getName()返回null。

时间:2017-01-18 09:55:23

标签: java android bluetooth

我使用Android应用程序获取硬件的名称,但我可以得到使用iOS的名称。我无法找到任何错误。所以我应该为这个问题做些什么。 在这里,我复制我的代码以供查找。

  

首先:

BluetoothManager bluetoothManager = (BluetoothManager)     
getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
        bluetoothAdapter.startDiscovery();
  

下一步:

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    getActivity().registerReceiver(mReceiver,filter);
    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    getActivity().registerReceiver(mReceiver,filter);
  

最后:

     public BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("走进receiver","bluetooth");
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already 
           if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
               String sname = device.getName();
           } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
      }
      }
    };

0 个答案:

没有答案