简体   繁体   English

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

[英]How to get android connected(not paired) bluetooth devices?

After the android phone is connected to the bluetooth device, the device cannot be searched. 将android手机连接到蓝牙设备后,无法搜索该设备。 How can I get the bluetooth devices connected to the phone?I tried several methods available online that are invalid. 如何将蓝牙设备连接到手机?我尝试了几种在线可用的无效方法。

This is the code that I'm working with. 这是我正在使用的代码。

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();
    }

here is my code: Sometime this may help you. 这是我的代码:有时候这可能对您有帮助。

1st you should check if Bluetooth has been connected with a device, 首先,您应该检查蓝牙是否已与设备连接,

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

If not connected you can use, 如果未连接,则可以使用,

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);
    }
}

This is onActivityResult to check your Bluetooth connection and connected devices. 这是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;
    }
}

This is the hanlder class : 这是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;
            }
        }

    };

The Handler always checks you are connected or not, If you are connected it will change isDeviceConnected = true; 处理程序总是检查您是否连接,如果您已连接,它将更改isDeviceConnected = true; else isDeviceConnected = false; 否则isDeviceConnected = false; if connected you can change your devices from the devices list or else you can continue with the same device. 如果已连接,则可以从设备列表中更改设备,否则可以继续使用同一设备。

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

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