如何获得android连接(未配对)的蓝牙设备?

时间:2018-08-14 01:43:33

标签: android bluetooth

将android手机连接到蓝牙设备后,无法搜索该设备。如何将蓝牙设备连接到手机?我尝试了几种在线可用的无效方法。

这是我正在使用的代码。

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    Class<BluetoothAdapter> bluetoothAdapterClass = 
BluetoothAdapter.class;
    try {
        Method method = 
bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
        method.setAccessible(true);
        int state = (int) method.invoke(adapter, (Object[]) null);

        if(state == BluetoothAdapter.STATE_CONNECTED){
            Log.i("BLUETOOTH","BluetoothAdapter.STATE_CONNECTED");
            Set<BluetoothDevice> devices = adapter.getBondedDevices();
            Log.i("BLUETOOTH","devices:"+devices.size());

            for(BluetoothDevice device : devices){
                Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
                method.setAccessible(true);
                boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
                if(isConnected){
                    Log.i("BLUETOOTH","connected:"+device.getName());
                    deviceList.add(device);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

这是我的代码:有时候这可能对您有帮助。

首先,您应该检查蓝牙是否已与设备连接

if (isDeviceConnected) {
    //do something
} else {
    //do something...
}

如果未连接,则可以使用

if (!mService.isBTopen()) {
    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
    if (isDeviceConnected) {
         Toast.makeText(getContext(), "Already Connected", Toast.LENGTH_SHORT).show();
    } else {
        Intent serverIntent = new Intent(getContext(), DeviceListActivity.class);
        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
    }
}

这是onActivityResult,用于检查您的蓝牙连接和连接的设备。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.i("Hello", " Device connection received!");

        switch (requestCode) {
            case REQUEST_ENABLE_BT:      //���������
                if (resultCode == Activity.RESULT_OK) {   //�����Ѿ���
                    Intent serverIntent = new Intent(getContext(), DeviceListActivity.class);
                    startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                } else {                 //�û������������
//                    finish();
                }
                break;
            case REQUEST_CONNECT_DEVICE:     //��������ijһ�����豸
                if (resultCode == Activity.RESULT_OK) {   //�ѵ�������б��е�ij���豸��
                String address = data.getExtras()
                        .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);  //��ȡ�б������豸��mac��ַ
                BluetoothDevice con_dev = mService.getDevByMac(address);
                String addressM = address;
                mService.connect(con_dev);
            }
        break;
    }
}

这是hanlder类:

private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case BluetoothService.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:   //������
                            Toast.makeText(getContext(), "Connect successful",
                                    Toast.LENGTH_SHORT).show();
                            isDeviceConnected = true;

//                            updateConnectBtn();
//                            btnClose.setEnabled(true);
//                            btnSendDraw.setEnabled(true);
                            break;
                        case BluetoothService.STATE_CONNECTING:  //��������
                            Log.d("��������", "��������.....");
                            break;
                        case BluetoothService.STATE_LISTEN:     //�������ӵĵ���
                        case BluetoothService.STATE_NONE:
                            Log.d("��������", "�ȴ�����.....");
                            break;
                    }
                    break;
                case BluetoothService.MESSAGE_CONNECTION_LOST:    //�����ѶϿ�����
                    Toast.makeText(getContext(), "Device connection was lost",
                            Toast.LENGTH_SHORT).show();
                    isDeviceConnected = false;
//                    btnClose.setEnabled(false);
//                    btnSendDraw.setEnabled(false);
                    break;
                case BluetoothService.MESSAGE_UNABLE_CONNECT:     //�޷������豸
                    Toast.makeText(getContext(), "Unable to connect device",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }

    };

处理程序始终检查您是否已连接,如果已连接,它将更改isDeviceConnected = true;,否则将更改isDeviceConnected = false;(如果已连接),则可以从设备列表中更改设备,或者可以继续进行操作设备。