Мне необходимо только передать пару строк на ардуино, ничего от нее получать не надо. Но проблема в том, как только я пытаюсь записать данные мне прилетает вот такое.
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
Собственно вот код, я впервые работаю с kotlin и android.
class Validation : AppCompatActivity() {
var mmOutputStream: OutputStream? = null
var mmSocket: BluetoothSocket? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_validation)
resolve_Btn.setOnClickListener {bluetoothAdd() }
}
@SuppressLint("HardwareIds")
private fun bluetoothAdd() {
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Log.e("Bluetooth ", "not found")
}
if (mBluetoothAdapter!!.isEnabled) {
val pairedDevices = mBluetoothAdapter.bondedDevices
// If there are paired devices
if (pairedDevices.size > 0) {
// Loop through paired devices
for (device in pairedDevices) {
mmSocket= device.createRfcommSocketToServiceRecord( UUID.fromString("00000000-0000-1000-8000-00805F9B34FB"))
mmOutputStream = mmSocket!!.outputStream
val message = "aaa"
val msgBuffer = message.toByteArray(Charset.defaultCharset())
mmOutputStream!!.write(msgBuffer)
Log.e("Mac Addressess", "are: " + mBluetoothAdapter.getRemoteDevice(device.address))
}
}
}
}
}
Найдено решение. Забыл открыть сокет.
mmSocket= device.createRfcommSocketToServiceRecord(
UUID.fromString("00000000-0000-1000-8000-00805F9B34FB"))
mmSocket.connect()
mmOutputStream = mmSocket!!.outputStream
val message = "aaa"
val msgBuffer = message.toByteArray(Charset.defaultCharset())
mmOutputStream!!.write(msgBuffer)
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости