2015-11-02 74 views
0

我需要創建一個超級簡單的應用,只需創建一個可檢測的藍牙LE信標,就不需要傳輸任何數據。我選擇了使用AltBeacon的lybrary,因此我使用他們提供的一個示例實現了應用程序。儘管如此,該應用程序崩潰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 我得到了積極的結果null檢查我的表現,所以我不知道我能做些什麼在我身邊。有沒有人對這個圖書館有任何麻煩?AltBeacon Beacon創建應用

下面的代碼是98%的例子available here。我正在使用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); 


    } 
} 

回答

3

您需要先檢查設備的外設模式支持。使用此代碼將有助於您ü

BluetoothManager btManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); 
      BluetoothAdapter btAdapter = btManager.getAdapter(); 
      if (btAdapter.isEnabled()) 
       boolean isSupported = btAdapter.isMultipleAdvertisementSupported()) ;