2016-03-15 45 views
6

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; 
} 
+0

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

+0

@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

+0

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

Trả lời

3

Vì lý do bảo mật, ứng dụng của bên thứ ba không thể kết nối với bàn phím bluetooth vì ứng dụng có thể là keylogger. Vì vậy, nó chỉ có thể được thực hiện bằng tay bởi người dùng.

0

Dưới đây là đoạn code tôi sử dụng trên Android M (6.0) .. Để có được một kết nối L2CAP bắt đầu (cần thiết cho HID)

public static BluetoothSocket createL2CAPBluetoothSocket(String address, int psm){ 
    return createBluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, false,false, address, psm); 
} 

// method for creating a bluetooth client socket 
private static BluetoothSocket createBluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address, int port){ 
    Log.e(TAG, "Creating socket with " + address + ":" + port); 

    try { 
     Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
       int.class, int.class,boolean.class,boolean.class,String.class, int.class); 
     constructor.setAccessible(true); 
     BluetoothSocket clientSocket = (BluetoothSocket) constructor.newInstance(type,fd,auth,encrypt,address,port); 
     return clientSocket; 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

public Boolean connect(View v) { 

    try { 
     // TODO: Check bluetooth enabled 
     mDevice = getController(); 

     if (mDevice != null) { 
      Log.e(TAG, "Controller is paired"); 

      // Create socket 
      mSocket = createL2CAPBluetoothSocket(mDevice.getAddress(), 0x1124); 

      if (mSocket != null) { 

       if (!mSocket.isConnected()) { 
        mSocket.connect(); 
       } 

       Log.e(TAG, "Socket successfully created"); 

       ConnectedThread mConnectedThread = new ConnectedThread(mSocket); 
       mConnectedThread.run(); 
      } 

     } else { 
      showToast("Controller is not connected"); 
     } 

     return true; 

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

     if (e instanceof IOException){ 
      // handle this exception type 
     } else { 
      // We didn't expect this one. What could it be? Let's log it, and let it bubble up the hierarchy. 

     } 

     return false; 
    } 
} 

private BluetoothDevice getController() { 
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 

    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
      if (device.getName().equals("Wireless Controller")) // Change to match DS4 - node name 
      { 
       Log.d(TAG, "Found device named: " + device.getName()); 

       return device; 
      } 
     } 
    } 

    return null; 
} 

Nó vẫn có thể có vấn đề tạo Dịch vụ, và bạn cần phải thiết lập LAMAP PSAM chính xác cho thiết bị, nhưng hy vọng nó có thể giúp ..

+0

Vì vậy, bạn có thể kết nối bàn phím bluetooth theo chương trình trên Android 6.0 – Passiondroid

Các vấn đề liên quan