获取UUID时,在Android 6.0请求配对与远程设备 [英] Android 6.0 requests pairing with remote devices when fetching UUIDS

查看:1801
本文介绍了获取UUID时,在Android 6.0请求配对与远程设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这样的远程蓝牙设备获取的UUID:

I'm trying to fetch the UUIDS from a remote bluetooth device like this:

        device.fetchUuidsWithSdp();

这将工作默默不上除了那些与Android 6.0这明显与配对对话框要求所有设备的用户交互与远程设备连接来获取UUID。那是一个预期的行为吗?这哪里是记录?是否有触发UUID没有发现有明确允许其从另一端的方式?

This will work silently and without user interaction on all devices except those with Android 6.0 which visibly ask with a pairing dialog to connect with the remote device to fetch the UUID. Is that an expected behaviour? Where is this documented? Is there a way to trigger UUID discovery without explicitly having to allow it from the other end?

推荐答案

我已经通过使用隐藏sdpSearch方法,而不是fetchUuidsWithSdp找到了解决办法了这一点。这一点需要一些反思。这为我工作在Android 6.0和5.1.1,但不尝试配对设备。
希望这会有所帮助,并随时改善比较差的异常处理。

I've found a workaround to this by using the hidden sdpSearch method instead of fetchUuidsWithSdp. This requires a bit of reflection. This worked for me on android 6.0 and 5.1.1, without the devices trying to pair. Hope this helps, and feel free to improve the rather poor exception handling.

public class DeviceFinder{

public interface Callback{
    void onDeviceFound(BluetoothDevice bd);
    void onFinishedCallback();
    void onStartCallback();
}

private ArrayList<BluetoothDevice> tempDevices = new ArrayList<>();
private Callback mCallback;
private Context mContext;
private String ACTION_SDP_RECORD;
private String EXTRA_SDP_SEARCH_RESULT;

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            // Aggregating found devices
            BluetoothDevice bd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            tempDevices.add(bd);
        }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
            // Prepare for new search
            tempDevices = new ArrayList<>();
            mCallback.onStartCallback();
        }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
            // Do a sdpSearch for all found devices
            for (BluetoothDevice bd : tempDevices){
                try {
                    Method m = bd.getClass().getDeclaredMethod("sdpSearch", ParcelUuid.class);
                    m.invoke(bd, new ParcelUuid(/* your uuid here */));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            mCallback.onFinishedCallback();
        }else if( ACTION_SDP_RECORD.equals(action)){
            // check if the device has the specified uuid
            BluetoothDevice bd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (intent.getIntExtra(EXTRA_SDP_SEARCH_RESULT, 1) == 0){
                mCallback.onDeviceFound(bd);
            }
        }
    }
};


public DeviceFinder(Context context, Callback mCallback){
    this.mCallback = mCallback;
    this.mContext = context;

    try {
        Field f = BluetoothDevice.class.getDeclaredField("ACTION_SDP_RECORD");
        ACTION_SDP_RECORD = ((String)f.get(null));
        f = BluetoothDevice.class.getDeclaredField("EXTRA_SDP_SEARCH_STATUS");
        EXTRA_SDP_SEARCH_RESULT = ((String)f.get(null));
    } catch (Exception e) {
        e.printStackTrace();
    }


    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    intentFilter.addAction(ACTION_SDP_RECORD);
    context.registerReceiver(mReceiver, intentFilter);
    startScan();
}

public void startScan(){
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!bluetoothAdapter.isDiscovering()) {
        bluetoothAdapter.startDiscovery();
    }
}

public void unregisterReciever(){
    mContext.unregisterReceiver(mReceiver);
}
}

编辑:
在在Android 6.0中添加sdpSearch,所以它并不适用于早期版本的工作

edit: sdpSearch was added in android 6.0, so it does not work for earlier versions

这篇关于获取UUID时,在Android 6.0请求配对与远程设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆