java - 扫描蓝牙设备

标签 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 list 中添加了以下权限:

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

在日志猫中它显示:

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

最佳答案

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

关于java - 扫描蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775295/

相关文章:

java - 来自不同口味的 fragment 膨胀

java - 简单jsp文件中的空请求响应

java - 无法从 war 中获取文件

android - Spotify API : INVALID_APP_ID

android - 为什么 getTheme 在应用程序上不能很好地工作

java - Android 的触摸次数翻倍?

android - 连接2个蓝牙设备

java - JR异常 : Invalid byte 1 of 1-byte UTF-8 sequence

Android 2.1 检测蓝牙音频连接/断开

iphone - 蓝牙应用的最佳平台