我正在尝试创建一个从Android 5.0设备传输信标的应用程序,但我收到此错误
{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 to start activity ComponentInfo{com.example.mohamed_pc.test2/com.example.mohamed_pc.test2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.BluetoothLeAdvertiser android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.BluetoothLeAdvertiser android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser()' on a null object reference
at com.example.mohamed_pc.test2.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) }
代码在
之下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) {
// do something when the button is 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
for (int i=2; i<=17; i++) {
mManufacturerData.put(i, uuid[i-2]); // adding the 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();
}
}
答案 0 :(得分:1)
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
给你null。检查permisses以及是否启用蓝牙
答案 1 :(得分:0)
更改格式:
UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020")
人:
UUID.fromString("0CF052C2-97CA-407C-84F8-B62AAC4E9020");