2014-10-27 26 views
0

您好我是android編程的新手,我在設備掃描活動中遇到問題。我想scan所有bluetooth devices在附近,並在UI上打印它們。但是我得到了與Receiving broadcast error for Bluetooth discovery相同的運行時錯誤。請,請幫我調試我的代碼。接收藍牙發現的廣播錯誤 - 通過startDiscovery()接收意圖的onRecieve()方法中的錯誤方法

public class MainActivity extends ActionBarActivity { 

    BluetoothAdapter mBluetoothAdapter; 
    private int REQUEST_ENABLE_BT = 1000; 
    public ArrayList<String> deviceArrayList = new ArrayList<>(); 
    public ArrayList<BluetoothDevice> deviceList = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      // Device does not support Bluetooth 
      Toast.makeText(this, "Device Doesn't Support Bluetooth!!!", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
     scanStart(); 
    } 

    public void scanStart() { 
     // TODO Auto-generated method stub 

     //Adding Paired Devices into ArrayList 
     addPairedDevices(); 

     if(mBluetoothAdapter.isDiscovering()){ 
      mBluetoothAdapter.cancelDiscovery(); 
     } 
     if(mBluetoothAdapter.startDiscovery() == true){ 
      Toast.makeText(this, "Searching...", Toast.LENGTH_LONG).show(); 
     } 
     else{ 
      Toast.makeText(this, "Bluetooth Adapter got Null Value", Toast.LENGTH_SHORT).show(); 
     } 

     // Register the BroadcastReceiver 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(requestReciever, filter); // Don't forget to unregister during onDestroy 

     try { 
      Thread.sleep(12000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //Printing Device List on UI 
     ListView lv = new ListView(this); 
     ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, R.layout.print_device_list, deviceArrayList); 
     lv.setAdapter(listAdapter); 
     setContentView(lv); 
     if(deviceArrayList.size()<=0){ 
      Toast.makeText(this, "No Devices Found...", Toast.LENGTH_LONG).show(); 
     } 
    } 


    public void addPairedDevices() { 
     // TODO Auto-generated method stub 
     // get paired devices 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a ListView 
       deviceArrayList.add(device.getName() + "\n" + device.getAddress() + "\n" + device.getUuids()[0].getUuid()); 
       deviceList.add(device); 
      } 
     } 
    } 


    private BroadcastReceiver requestReciever = new BroadcastReceiver(){ 

     public void onReceive(Context context, Intent intent) { 
      if (intent == null) { 
       return; 
      } 
      String action = intent.getAction(); 

      if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
       Log.d("BT", "Device Found"); 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if(device!=null){ 
        if(device.getName() != null && device.getName().length() > 0){ 
         deviceArrayList.add(device.getName()+"\n"+device.getAddress()+"\n"+device.getUuids()[0].getUuid()); 
         deviceList.add(device); 
        } 
       } 
      } 
     }; 
    }; 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode == REQUEST_ENABLE_BT){ 
      if(resultCode == Activity.RESULT_CANCELED){ 
       Toast.makeText(this, "Uesr Doesn't want to switch on Bluetooth!!!", Toast.LENGTH_LONG).show(); 
       return; 
      } 
     } 
    } 



    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     if(mBluetoothAdapter != null){ 
      mBluetoothAdapter.cancelDiscovery(); 
     } 
     if(requestReciever!=null) 
      unregisterReceiver(requestReciever); 
     Toast.makeText(this, "Bluetooth App Stopped", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

在這裏發佈您的錯誤日誌。 – Akhil 2014-10-27 10:11:19

+0

Thanx For Reply ...錯誤日誌與本文相同 - http://stackoverflow.com/questions/10376388/receiving-broadcast-error-for-bluetooth-discovery – doga 2014-10-27 10:38:03

回答

0

device.getUuids() - 在這裏你有一個空,這就是爲什麼你在這裏有一個錯誤。 使用它檢查此變量。