簡體   English   中英

掃描藍牙設備

[英]Scanning Bluetooth Devices

在這段代碼中,當單擊按鈕時,我正在掃描藍牙設備,但沒有發現任何發現的設備。

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

您的清單中缺少ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION權限。 看看這個 如果您不解釋您為何需要此權限,則用戶可能會拒絕此權限,因此在請求權限之前向他們顯示說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM