java - ble beacon transmitter app close once it run -
i trying create app transmit beacons android 5.0 devices error
{09-07 10:23:45.184 3057-3057/com.example.mohamed_pc.test2 e/androidruntime: fatal exception: main process: com.example.mohamed_pc.test2, pid: 3057 java.lang.runtimeexception: unable start activity componentinfo{com.example.mohamed_pc.test2/com.example.mohamed_pc.test2.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.bluetooth.le.bluetoothleadvertiser android.bluetooth.bluetoothadapter.getbluetoothleadvertiser()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.bluetooth.le.bluetoothleadvertiser android.bluetooth.bluetoothadapter.getbluetoothleadvertiser()' on null object reference @ com.example.mohamed_pc.test2.mainactivity.oncreate(mainactivity.java:31) @ android.app.activity.performcreate(activity.java:6237) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) }
the code below
package com.example.mohamed_pc.test2; import android.bluetooth.bluetoothadapter; import android.bluetooth.le.advertisecallback; import android.bluetooth.le.advertisedata; import android.bluetooth.le.advertisesettings; import android.bluetooth.le.bluetoothleadvertiser; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import java.nio.bytebuffer; import java.util.uuid; public class mainactivity extends appcompatactivity { private bluetoothadapter mbluetoothadapter; private bluetoothleadvertiser mbluetoothleadvertiser; private advertisedata madvertisedata; private advertisesettings madvertisesettings; private advertisecallback madvertisecallback; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mbluetoothadapter = bluetoothadapter.getdefaultadapter(); mbluetoothleadvertiser = mbluetoothadapter.getbluetoothleadvertiser(); } public void buttononclick(view v) { // when button clicked button button=(button) v; ((button) v).settext("clicked"); setadvertisedata(); setadvertisesettings(); mbluetoothleadvertiser.startadvertising(madvertisesettings, madvertisedata, madvertisecallback); } protected void setadvertisedata() { advertisedata.builder mbuilder = new advertisedata.builder(); bytebuffer mmanufacturerdata = bytebuffer.allocate(24); byte[] uuid = getidasbyte(uuid.fromstring("0cf052c297ca407c84f8b62aac4e9020")); mmanufacturerdata.put(0, (byte)0xbe); // beacon identifier mmanufacturerdata.put(1, (byte)0xac); // beacon identifier (int i=2; i<=17; i++) { mmanufacturerdata.put(i, uuid[i-2]); // adding uuid } mmanufacturerdata.put(18, (byte)0x00); // first byte of major mmanufacturerdata.put(19, (byte)0x09); // second byte of major mmanufacturerdata.put(20, (byte)0x00); // first minor mmanufacturerdata.put(21, (byte)0x06); // second minor mmanufacturerdata.put(22, (byte)0xb5); // txpower mbuilder.addmanufacturerdata(224, mmanufacturerdata.array()); // using google's company id madvertisedata = mbuilder.build(); } protected void setadvertisesettings() { advertisesettings.builder mbuilder = new advertisesettings.builder(); mbuilder.setadvertisemode(advertisesettings.advertise_mode_low_power); mbuilder.setconnectable(false); mbuilder.settimeout(0); mbuilder.settxpowerlevel(advertisesettings.advertise_tx_power_medium); madvertisesettings = mbuilder.build(); } public static byte[] getidasbyte(java.util.uuid uuid) { bytebuffer bb = bytebuffer.wrap(new byte[16]); bb.putlong(uuid.getmostsignificantbits()); bb.putlong(uuid.getleastsignificantbits()); return bb.array(); } }
mbluetoothadapter = bluetoothadapter.getdefaultadapter();
is giving null. check permisses , if bluetooth enabled
Comments
Post a Comment