Nie wiem, czy to nadal jest problem, czy nie, ale znalazłem, że przy użyciu metody connect
w odbiciu połączenia działa. Praca off z kodem, który pmont używane z linku w odpowiedzi Lorelorelore „s:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Method mBTPanConnect;
try {
classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
mBTPanConnect = classBluetoothPan.getDeclaredMethod("connect", BluetoothDevice.class);
BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
BTPanCtor.setAccessible(true);
BTSrvInstance = BTPanCtor.newInstance(myContext, new BTPanServiceListener(myContext));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
try{
mBTPanConnect.invoke(BTSrvInstance, device);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Oczywiście, to zakłada, że Bluetooth jest włączony, a masz tylko jedno powiązane urządzenie. Ale włączenie bluetooth jest dość proste przy użyciu standardowych (nie refleksyjnych) wywołań, a możesz po prostu sprawdzić sparowane urządzenie, z którym chcesz się połączyć w pętli for
. Nie zapomnij także o klasie BTPanServiceListener
z drugiej odpowiedzi.
Mam nadzieję, że to pomoże.
To nie działa dla mnie. Zaimplementowałem to i nic się nie dzieje – Bek
Czy dodałeś wszystkie uprawnienia do manifestu? – Lorelorelore
yes already added – Bek