Tôi có thể ghép nối bàn phím bluetooth nhưng không thể kết nối để làm cho thiết bị đó trở thành thiết bị đầu vào. Tôi đã xem tài liệu được cung cấp tại trang web dành cho nhà phát triển - http://developer.android.com/guide/topics/connectivity/bluetooth.html#ProfilesCách lập trình ghép nối và kết nối thiết bị bluetooth HID (Bàn phím Bluetooth) trên Android
Nó nói rằng API Bluetooth của Android cung cấp triển khai cho các cấu hình Bluetooth sau đây nhưng bạn có thể triển khai giao diện BluetoothProfile để viết các lớp của riêng bạn.
- Tai nghe
- A2DP
- Device Sức khỏe
Không có tài liệu hướng dẫn làm thế nào để thực hiện Cấu hình Bluetooth cho điện thoại bluetooth HID (Keyboard)
Android đã tự thực hiện kết nối bluetooth cho các thiết bị HID nhưng các API đó bị ẩn. Tôi đã cố gắng phản ánh để sử dụng chúng quá. Tôi không nhận được bất kỳ lỗi nào nhưng bàn phím không được kết nối như thiết bị đầu vào. Đây là những gì tôi đã làm -
private void connect(final BluetoothDevice bluetoothDevice) {
if(bluetoothDevice.getBluetoothClass().getDeviceClass() == 1344){
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.i("btclass", profile + "");
if (profile == getInputDeviceHiddenConstant()) {
Class instance = null;
try {
//instance = Class.forName("android.bluetooth.IBluetoothInputDevice");
instance = Class.forName("android.bluetooth.BluetoothInputDevice");
Method connect = instance.getDeclaredMethod("connect", BluetoothDevice.class);
Object value = connect.invoke(proxy, bluetoothDevice);
Log.e("btclass", value.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(int profile) {
}
};
mBluetoothAdapter.getProfileProxy(this, mProfileListener,getInputDeviceHiddenConstant());
}
}
public static int getInputDeviceHiddenConstant() {
Class<BluetoothProfile> clazz = BluetoothProfile.class;
for (Field f : clazz.getFields()) {
int mod = f.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
try {
if (f.getName().equals("INPUT_DEVICE")) {
return f.getInt(null);
}
} catch (Exception e) {
Log.e("", e.toString(), e);
}
}
}
return -1;
}
Bạn đã quản lý để có được một kết nối HID làm việc? Im bị mắc kẹt với các vấn đề mà phiên bản Android mới hơn không có hồ sơ HID HID. Bạn đã sử dụng phiên bản Android nào? – DutchKevv
@DutchKev - Tôi đã thử trên Android 4.4.2 và khi tôi trả lời dưới đây, bạn không thể kết nối nó theo chương trình. Ngoài ra tôi không nhận thức được phiên bản Android mới hơn cho dù họ cung cấp hồ sơ HID HID hay không. – Passiondroid
Cảm ơn hoặc trả lời. xem mã dưới đây .. Tôi tiếp tục làm việc trên cùng một vấn đề đêm qua, và có một vài bước xa hơn .. Khi tôi lần đầu tiên kết nối 'thủ công' máng thông thường xử lý kết nối bluetooth Android .. Tôi có thể sau đó, với sự phản ánh, tạo ra một L2CAP Ổ cắm bluetooth – DutchKevv