Mam trochę kodu, aby automatycznie sparować urządzenie bluetooth, wywołując funkcję createBond(), rejestrując odbiornik dla android.bluetooth. device.action.PAIRING_REQUEST, a następnie ręcznie wprowadź kod PIN, aby sparować.Android.bluetooth.IBluetooth.createBond() nie znaleziono w 4.2.1, ale działa we wcześniejszych wersjach OS
To było wspaniałe z wszystkich urządzeń testowanych do tej pory do Andoid 4.0 ale dzisiaj próbowałem to na moim Nexusie 7 z Androidem 4.2.1 i uzyskałem następujący błąd:
java.lang.noSuchMethodException: Android. bluetooth.IBluetooth.createBond
Czy rzeczywiście usunięto tę funkcję z biblioteki?
UPDATE
Co się rzeczywiście dzieje jest przedmiotem interfejs IBluetooth że używam zadzwonić createBond nie jest zainicjowany. W poniższym kodzie linia, która próbuje uzyskać identyfikator IBlink o nazwie BTBinder, zwraca wartość null, gdy ten proces kończy się niepowodzeniem, powodując ustawienie wartości BTInterface na wartość null na końcu. Tak więc moje pytanie brzmi teraz: dlaczego na moim Nexusie 7 z Androidem 4.2.1 wywołanie, które spowodowało zwrócenie miotnika, działa poprawnie na 5 innych testowanych przeze mnie urządzeniach?
public static IBluetooth getBluetoothInterface()
{
//Gets a bluetooth interface from private Android system API
IBluetooth BTInterface = null;
try
{
Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
Method getService = ServiceManager.getDeclaredMethod("getService", String.class);
IBinder BTBinder = (IBinder) getService.invoke(null, "bluetooth");
Class<?> IBluetooth = Class.forName("android.bluetooth.IBluetooth");
Class<?>[] IBluetoothClasses = IBluetooth.getDeclaredClasses();
Class<?> IBluetoothClass0 = IBluetoothClasses[0];
Method asInterface = IBluetoothClass0.getDeclaredMethod("asInterface",IBinder.class);
asInterface.setAccessible(true);
BTInterface = (IBluetooth) asInterface.invoke(null, BTBinder);
}
catch (Exception e)
{
return null;
}
return BTInterface;
}
Jestem bardzo zainteresowany problemem. Jeśli masz jakieś wiadomości, udostępnij je: s. – castrogne