android - can we pass real time data of ble4.0 to next activity -
i trying use ble 4.0 data 1 activity other it's not working me because real time data.
private void displaydata(string data) { if (data != null) { mdatafield.settext(data); } }
i want use data next activity creating graphview
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main3); d1=(textview) findviewbyid(r.id.datavalue); intent intent = getintent(); string data1 = intent.getstringextra("data"); displaydata(data1); //intent gattserviceintent = new intent(this, rblservice.class); //bindservice(gattserviceintent, mserviceconnection, bind_auto_create); line.clear(); }
instead of passing data between activities, have 1 activity, displaying several fragments:
- here google doc - building dynamic ui fragments
- and nice 3rd party book - creating dynamic ui android fragments.
then put ble managing source code 1 , activity.
here example of such class (make activity implement bleobject.blelistener
, instantiate bleobject
in mbleobject = new bleobject(this, this)
):
public class bleobject { private context mcontext; private blelistener mlistener; private handler mrssireader; private handler mrssiwatchdog; private bluetoothadapter mbluetoothadapter; private bluetoothgatt mbluetoothgatt; // let activity implement interface (and manage fragments) public interface blelistener { public void devicefound(bluetoothdevice device, int rssi); public void updaterssi(bluetoothdevice device, int rssi); // add more callbacks here, example discovered services } public bleobject(context context, blelistener listener) { mcontext = context; mlistener = listener; mrssireader = new handler(); mrssiwatchdog = new handler(); } // method should called before using bleobject public boolean checkhardware() { bluetoothmanager bluetoothmanager = (bluetoothmanager) mcontext.getsystemservice(context.bluetooth_service); mbluetoothadapter = bluetoothmanager.getadapter(); if (mbluetoothadapter == null) return false; return mcontext.getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le); } public boolean isenabled() { return mbluetoothadapter.isenabled(); } private bluetoothadapter.lescancallback mscancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, final int rssi, byte[] scanrecord) { if (device == null) return; string address = device.getaddress(); if (!bluetoothadapter.checkbluetoothaddress(address)) return; mlistener.devicefound(device, rssi); } }; private final bluetoothgattcallback mgattcallback = new bluetoothgattcallback() { @override public void onconnectionstatechange(bluetoothgatt gatt, int status, int newstate) { if (newstate == bluetoothprofile.state_connected) { readperiodicalyrssi(); } else if (newstate == bluetoothprofile.state_disconnected) { disconnect(); } } @override public void onreadremoterssi(bluetoothgatt gatt, int rssi, int status) { if (gatt == null) return; bluetoothdevice device = gatt.getdevice(); if (device == null) return; if (status == bluetoothgatt.gatt_success) { mlistener.updaterssi(device, rssi); } } }; private runnable mwatchdogrunnable = new runnable() { @override public void run() { mgattcallback.onreadremoterssi(mbluetoothgatt, -666, bluetoothgatt.gatt_success); } }; private runnable mrssirunnable = new runnable() { @override public void run() { if (mbluetoothgatt == null) return; mbluetoothgatt.readremoterssi(); readperiodicalyrssi(); } }; private void readperiodicalyrssi() { mrssireader.postdelayed(mrssirunnable, 1000); mrssiwatchdog.removecallbacks(mwatchdogrunnable); mrssiwatchdog.postdelayed(mwatchdogrunnable, 10000); } @suppresswarnings("deprecation") public void startscanning() { mbluetoothadapter.startlescan(mscancallback); } @suppresswarnings("deprecation") public void stopscanning() { mbluetoothadapter.stoplescan(mscancallback); } public void connect(string address) { connect(mbluetoothadapter.getremotedevice(address)); } public void connect(final bluetoothdevice device) { handler handler = new handler(); handler.post(new runnable() { @override public void run() { mbluetoothgatt = device.connectgatt(mcontext, true, mgattcallback); } }); } public void disconnect() { try { mbluetoothgatt.disconnect(); } catch (exception e) { } try { mbluetoothgatt.close(); } catch (exception e) { } mbluetoothgatt = null; } }
Comments
Post a Comment