简体   繁体   English

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)

[英]How to Check if an Paired Bluetooth device is a Printer or a Scanner (Android)

I hope can help me, I am developing an Android App, that require to connect with Bluetooth devices, like Scanner and Printer, currently I can list all Paired devices, but i want to know if the paired device is a Printer, Scanner, Mobile, etc. 我希望能在开发一个Android应用程序时对我有所帮助,该应用程序需要连接蓝牙设备(例如扫描仪和打印机),目前我可以列出所有配对的设备,但是我想知道配对的设备是否是打印机,扫描仪,移动设备等

There is some way to know the kind of bluetooth device paired? 有某种方法可以知道配对的蓝牙设备的种类?

In short, 简而言之,

Yes you can. 是的你可以。 You can do this by using the UUID of the device. 您可以通过使用设备的UUID来执行此操作。 If you know the UUID of a device you can match them up from the reported UUID and know which paired device is what. 如果您知道设备的UUID,则可以从报告的UUID中将它们匹配,并知道哪个配对的设备是什么。

Something like this: 像这样:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);

ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);

Now simply compare the retrieved UUID to the devices known UUID( online or on the box). 现在,只需将检索到的UUID与已知的UUID(在线或盒装)进行比较即可。

If they are a match you know what device it is. 如果它们匹配,您就会知道它是什么设备。

Note: most common UUID (scanners, printers, Mice) have the generic UUID 0001101-0000-1000-8000-00805F9B34FB 注意:最常见的UUID(扫描仪,打印机,鼠标)具有通用UUID 0001101-0000-1000-8000-00805F9B34FB

Read about the getUUID() method, paracable method , Method java class and finally Java.util.UUID. 阅读有关getUUID()方法, paracable方法, Method java类以及最后Java.util.UUID的信息。

I have one idea, it may help you. 我有一个主意,可能会对您有所帮助。

for (BluetoothDevice device : pairedDevices)
{
    String deviceBTMajorClass = getBTMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass());
    if (D) Log.d(TAG, "deviceBTMajorClass"+deviceBTMajorClass);
    //btArrayAdapter.add(deviceBTName + "\n"+ deviceBTMajorClass);
    data.add(device.getName() + "\n" + device.getAddress());
}

private String getBTMajorDeviceClass(int major) {
    switch (major) {
        case BluetoothClass.Device.Major.AUDIO_VIDEO:
            return "AUDIO_VIDEO";
        case BluetoothClass.Device.Major.COMPUTER:
            return "COMPUTER";
        case BluetoothClass.Device.Major.HEALTH:
            return "HEALTH";
        case BluetoothClass.Device.Major.IMAGING:
            return "IMAGING";
        case BluetoothClass.Device.Major.MISC:
            return "MISC";
        case BluetoothClass.Device.Major.NETWORKING:
            return "NETWORKING";
        case BluetoothClass.Device.Major.PERIPHERAL:
            return "PERIPHERAL";
        case BluetoothClass.Device.Major.PHONE:
            return "PHONE";
        case BluetoothClass.Device.Major.TOY:
            return "TOY";
        case BluetoothClass.Device.Major.UNCATEGORIZED:
            return "UNCATEGORIZED";
        case BluetoothClass.Device.Major.WEARABLE:
            return "AUDIO_VIDEO";`enter code here`
        default:
            return "unknown!";
    }
}

Accepted answer is too generic, because UUID 0001101-0000-1000-8000-00805F9B34FB assert that device has serial port capabilites, and many not printer devices has this kind of UUID. 接受的答案太笼统,因为UUID 0001101-0000-1000-8000-00805F9B34FB断言设备具有串行端口功能,并且许多非打印机设备都具有这种UUID。

You should evalute full CoD, at this link you can extract exact bit mask for serivice you need to recognize, that for a printer is : 您应该评估完整的CoD,在此链接上,您可以为需要识别的服务提取准确的位掩码,对于打印机,该掩码为:

0b000001000000011010000000

with this you can do a mask on full device CoD as: 这样,您可以在完整设备的CoD上执行以下操作:

private static boolean isAPrinter(BluetoothDevice device){
        int priterMask = 0b000001000000011010000000;
        int fullCod = device.getBluetoothClass().hashCode();
        Log.d(TAG, "FULL COD: " + fullCod);
        Log.d(TAG,"MASK RESULT " + (fullCod & priterMask));
        return (fullCod & priterMask) == priterMask;
      }

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

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