扫描蓝牙设备

时间:2018-08-09 20:27:36

标签: java android bluetooth

在此代码中,当单击按钮时,我正在扫描蓝牙设备,但没有得到任何发现的设备。

public class MainActivity extends AppCompatActivity {

    private static final int REQUESTCODE =200 ;
    BluetoothAdapter bluetoothAdapter;
    private boolean isDeviceEnabled=false;
    Button button;

    BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            Log.d("TAG","DSSSSS");

            if (BluetoothDevice.ACTION_FOUND.equals(action)){

                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d("TAG ","Unknown Device address "+device.getAddress());
                Log.d("TAG ","Unknown Device name "+device.getName());

                bluetoothAdapter.cancelDiscovery();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               scanBluetoothdevice();
            }
        });
       bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

       if (null==bluetoothAdapter){
           Toast.makeText(this, "Bluetooth Missing", Toast.LENGTH_SHORT).show();
       }else{
           if (!bluetoothAdapter.isEnabled()){
               Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
               startActivityForResult(intent,REQUESTCODE);

           }else {
               noOfPairedDevices();
           }
       }
    }

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

        if (requestCode==REQUESTCODE && resultCode==RESULT_OK){

          noOfPairedDevices();
        }else {
            Toast.makeText(this,"BlueTooth Off",Toast.LENGTH_SHORT).show();
        }
    }

    private  void noOfPairedDevices(){

        Set<BluetoothDevice> bluetoothDevices=bluetoothAdapter.getBondedDevices();
        if (bluetoothDevices.size() >0){

            for (BluetoothDevice device :bluetoothDevices){
                Log.d("Tag","Paired Device address="+device.getAddress());
                Log.d("Tag","Paired Device name="+device.getName());
            }
        }
    }

    private  void scanBluetoothdevice(){

        IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
        if (!isDeviceEnabled){

            this.registerReceiver(broadcastReceiver,filter);
            bluetoothAdapter.startDiscovery();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(broadcastReceiver);
        bluetoothAdapter.cancelDiscovery();
    }
}

我还在Android清单中添加了以下权限:

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

在Log cat中,它显示:

08-10 01:28:50.146 25450-25463/dead.bluetooth D/BluetoothAdapter: onBluetoothStateChange: up=true
    onBluetoothStateChange: Bluetooth is on
08-10 01:28:50.266 25450-25450/dead.bluetooth D/Tag: Paired Device address=BC:D1:1F:AB:1E:C1
08-10 01:28:50.276 25450-25450/dead.bluetooth D/Tag: Paired Device name=Galaxy J5 TULSI@avenger
    Paired Device address=CC:73:14:85:D5:49
08-10 01:28:50.316 25450-25450/dead.bluetooth D/Tag: Paired Device name=Cloud S9
08-10 01:28:50.356 25450-25450/dead.bluetooth I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9f162ab time:16915604
08-10 01:28:53.896 25450-25450/dead.bluetooth D/ViewRootImpl: ViewPostImeInputStage processPointer 0
08-10 01:28:53.986 25450-25450/dead.bluetooth D/ViewRootImpl: ViewPostImeInputStage processPointer 1
08-10 01:28:53.986 25450-25450/dead.bluetooth D/BluetoothAdapter: startDiscovery
08-10 01:28:53.986 25450-25450/dead.bluetooth D/BluetoothAdapter: startDiscovery = true

1 个答案:

答案 0 :(得分:0)

您的清单中缺少ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限。看一下this。如果您不解释为什么需要此权限,用户可能会拒绝此权限,因此请在请求权限之前向他们显示说明。