Android app doesn't connect to a Bluetooth Low Energy device -
i'm starting android development, , i'm trying make simple bluetooth low energy connection between android phone , microcontroller (psoc4ble) write value in 1 of microcontroller characteristics.
as know microcontroller mac, , service , characteristic uuid, want android app opens, makes connection microcontroller without user interaction, , when press button, app writes value characteristic.
the problem app crash when run it, , in cases when tweaking code work, doesn't connect microcontroller, i'm doing wrong?
here code:
public class mainactivity extends activity { // bluetooth variables private bluetoothadapter mbluetoothadapter; private bluetoothgatt mbluetoothgatt; private bluetoothgattcharacteristic mmycharacteristic; private static final uuid car_service = uuid.fromstring("00000000-0000-1000-1000-100000000000"); private static final uuid car_characteristic = uuid.fromstring("00000000-0000-2000-2000-200000000000"); private final bluetoothgattcallback mgattcallback = new bluetoothgattcallback() { }; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // initialize bluetooth adapter final bluetoothmanager bluetoothmanager = (bluetoothmanager) getsystemservice(context.bluetooth_service); mbluetoothadapter = bluetoothmanager.getadapter(); // create device represents bluetooth device , assign it's known mac address final bluetoothdevice device = mbluetoothadapter.getremotedevice("00:a0:50:0f:13:1c"); // make connection device mbluetoothgatt = device.connectgatt(mainactivity.this, false, mgattcallback); // reference user interface button button btnwrite = (button)findviewbyid(r.id.btnwrite); // define button behaviour btnwrite.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // create characteristic variable byte[] value = new byte[1]; value[0] = (byte) 1; mmycharacteristic.setvalue(value); // set service uuid , characteristic uuid mmycharacteristic = mbluetoothgatt.getservice(car_service).getcharacteristic(car_characteristic); // write characteristic en device mbluetoothgatt.writecharacteristic(mmycharacteristic); } }); }
}
update 1 logcat, , says:
java.lang.runtimeexception: unable start activity componentinfo{lenovo.car/lenovo.car.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.bluetooth.bluetoothdevice android.bluetooth.bluetoothadapter.getremotedevice(java.lang.string)' on null object reference caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.bluetooth.bluetoothdevice android.bluetooth.bluetoothadapter.getremotedevice(java.lang.string)' on null object reference
please, idea? found app crash @ final bluetoothdevice device = mbluetoothadapter.getremotedevice("00:a0:50:0f:13:1c");
you not connect remote device actually.
// make connection device mbluetoothgatt = device.connectgatt(mainactivity.this, false, mgattcallback);
obtaining bluetoothgatt instance not enought, need invoke mbluetoohgatt.connect()
method on gatt object.
notice mbluetoohgatt.connect()
returns boolean indicating whether connection request sent.
furthermore, in next step connection state should changed. notified via
mgattcallback.onconnectionstatechange() method.
once connection established should send request in order obtain device services. available via
mbluetoothgatt.discoverservices method.
if services discovery request succeeds should receive services of device.
you can obtain services , characteristics of device. can assume @ point connected remote device on condition device not require authentication.
in order read current value under specific characteristic must
call mbluetoothgatt.readcharacteristic() applying characteristic value of want find out.
the characteristic value inserted should notified via mgattcallback.oncharacteristicread(). works under condition characteristic value requesting readable.
in order write current value under specific characteristic must
call mbluetoothgatt.writecharacteristic() applying characteristic new value.
as confirmation of request should notification via mbluetoothgatt.oncharacteristicwrite(). works if characteristic writable.
hope helps somehow.
Comments
Post a Comment