Reputation: 855
I need to create a super simple app that just creates a detectable Bluetooth LE beacon, there is no need to transmit any data. I have chosen to use the AltBeacon lybrary, and as such I implemented the app with one of the examples they provide. Still, the app crashes with java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.le.BluetoothLeAdvertiser.startAdvertising(android.bluetooth.le.AdvertiseSettings, android.bluetooth.le.AdvertiseData, android.bluetooth.le.AdvertiseCallback)' on a null object reference
I get positive result on both null
checks I perform, so I'm not sure what else I could do on my side. Has anyone had any trouble with this library?
The code bellow is 98% the example available here. I'm using Android 5.0.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.BeaconTransmitter;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
BeaconTransmitter beaconTransmitter;
Beacon beacon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beacon = new Beacon.Builder()
.setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
.setId2("1")
.setId3("2")
.setManufacturer(0x0118)
.setTxPower(-59)
.setDataFields(Arrays.asList(new Long[]{0l}))
.build();
if(beacon==null)
Toast.makeText(getApplicationContext(), "NULL beacon", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "OK beacon", Toast.LENGTH_LONG).show();
BeaconParser beaconParser = new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
if(beaconTransmitter==null)
Toast.makeText(getApplicationContext(), "NULL beacon trasmitter", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "OK beacon trasmitter", Toast.LENGTH_LONG).show();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
beaconTransmitter.startAdvertising(beacon);
}
}, 5000);
}
}
Upvotes: 0
Views: 1289
Reputation: 46
I know this is an old post and it might not be related but remember to turn on Bluetooth! That was the reason that threw an error at me!
Upvotes: 0
Reputation: 730
you need to check peripheral mode support of your device first. using this code will help u
BluetoothManager btManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = btManager.getAdapter();
if (btAdapter.isEnabled())
boolean isSupported = btAdapter.isMultipleAdvertisementSupported()) ;
Upvotes: 4