У меня есть карманный принтер bluetooth, с которым я могу связаться с использованием SPP-соединения с моего Mac (используя Coolterm). Когда я пытаюсь сделать то же самое с Android (используя платформу 7), у меня возникает несколько проблем:
-
Принтер, похоже, не поддерживает/не требует аутентификации PIN-кода. При подключении с OSX я просто выбрал вариант, который сказал "Не использовать штырь", и он попал в паре. В Android, когда я использую
device.createRfcommSocketToServiceRecord()
, он всегда заканчивает тем, что спрашивает у меня PIN/Ключ (которого у меня нет/необходимости). Я решил это с помощью трюка отражения:Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); BluetoothSocket connection = (BluetoothSocket) m.invoke(device, 1);
Я не уверен, что это действительно сработало, но мигающий светодиод на принтере перестает мигать, что заставляет меня поверить в это.
-
Как только у меня есть сокет, я пытаюсь записать байтовые данные в поток, используя:
byte[] buffer = new byte[3]; buffer[0] = (byte) 0x8A; buffer[1] = (byte) 0xC1; buffer[2] = (byte) 0x04; outStream.write(buffer); int response = inStream.read(); mySocket.close();
Отправка одной трехбайтовой последовательности из Coolterm на OSX распечатала тестовую страницу с принтера. Однако, похоже, этот поток зависает на Android (чтение).
Есть ли что-то, что я упускаю здесь?
EDIT: Кажется, это работает только тогда, когда я устанавливаю канал на 1. Таким образом, это означает, что я здесь что-то здесь.