【问题标题】:Get Bluetooth device information from the Android bluetooth device picker从 Android 蓝牙设备选择器获取蓝牙设备信息
【发布时间】:2017-01-30 17:57:48
【问题描述】:

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

How to retrieve Bluetooth device info with Android Bluetooth device picker?

Android Bluetooth Device Picker Usage

设备选择器确实显示,但我无法找出在我的代码中选择了哪个设备。 onReceive里面的toast没有显示,说明没有收到广播。

我遇到的另一个问题是,如果我尝试在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));

}

任何帮助将不胜感激。

注意:我在运行 Marshmallow 的 Lenovo K3 note 上测试了代码。

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多