繁体   English   中英

从Android蓝牙设备选择器中获取蓝牙设备信息

[英]Get Bluetooth device information from the Android bluetooth device picker

我正在开发一个Android应用程序,其中涉及向用户显示附近的蓝牙设备列表,并连接到由他们选择的设备。 我正在尝试使用这些帖子中所示的系统蓝牙设备选择器:

如何使用Android蓝牙设备选择器检索蓝牙设备信息?

Android蓝牙设备选择器的使用

设备选择器确实显示了,但是我找不到我的代码中选择了哪个设备。 没有显示onReceive内的祝酒词,这表明未接收到广播。

我遇到的另一个问题是,如果我尝试在onRequestPermissionsResult内部启动设备选择器活动,即使在请求许可权对话框中单击“允许”,设备选择器也不会显示。 里面的吐司也不显示。

这是代码:

//Code inside Fragment
BroadcastReceiver mReceiver;
BluetoothAdapter bluetoothAdapter;
BluetoothSocket bsock;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.controller_mode_layout,container,false);

     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
//Get location access permission.
    if (bluetoothAdapter != null) {
        if (bluetoothAdapter.isEnabled()) {
            ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, reqCode);
        }
    }
//Receiver to get the selected device information 
    mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context,"Device"+device.getAddress(), Toast.LENGTH_SHORT).show();
            try {
                bsock=device.createRfcommSocketToServiceRecord(UUID.fromString("00002415-0000-1000-8000-00805F9B34FB"));
                bsock.connect();
                //Send and receive data logic follows

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    getActivity().registerReceiver(mReceiver, new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED"));

    showDevicePicker();

    return myView;
}



@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
{
    Toast.makeText(getActivity(),"Permission result", Toast.LENGTH_SHORT).show();
    if((requestCode == reqCode) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
    {
        //Not working
       // showDevicePicker();
    }
}

public void showDevicePicker()
{
    //Launch built in bluetooth device picker activity
    startActivity( new Intent("android.bluetooth.devicepicker.action.LAUNCH")
            .putExtra("android.bluetooth.devicepicker.extra.NEED_AUTH", false)
            .putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 0)
            .putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.example.ankit2.controllerapp1")
            .putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.example.ankit2.controllerapp1.Fragment1")
            .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));

}

任何帮助将不胜感激。

注意:我在运行棉花糖的Lenovo K3笔记上测试了代码。

问题出在DEVICE_PICKER_LAUNCH_CLASS ,它指定为Fragment1 即使广播接收器位于片段中,启动类也必须是活动。 在这种情况下,将启动类更改为ControllerActivity (其中包含片段Fragment1 )解决了该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM