連線電控
連線
/**
* withMeta default: true
*/
FarmlandBikeKit.sleipnir().connectDevice(
context = context,
bluetoothPeripheral = bluetoothPeripheral,
withMeta = true
)在此使用到的參數是
BluetoothPeripheral的實例,可參閱 掃描電控 。
withMeta為是否要在連線時自動讀取 Meta,如為true,則會在讀取完後才會收到DeviceState.Prepared。
斷線
FarmlandBikeKit.sleipnir().bluetoothDisconnect(context, bluetoothPeripheral)連線狀態
FarmlandBikeKit.sleipnir().bluetoothPeripheralPublisher
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
when(it) {
is DeviceState.Connected -> { state: DeviceState.Connected ->
// 可從 state.peripheral 獲取已連線成功的 BluetoothPeripheral 物件
}
is DeviceState.Prepared -> { state: DeviceState.Prepared ->
// 裝置已進入準備狀態,可從 state.peripheral 獲取已準備好的 BluetoothPeripheral 物件
}
is DeviceState.Disconnected -> { state: DeviceState.Disconnected ->
// 裝置已進入斷線,可從 state.peripheral 獲取已斷線的 BluetoothPeripheral 物件
}
else -> Unit
}
}透過對 bluetoothPeripheralPublisher 的監聽,可以觀察電控連線狀態的即時變化。