2012-06-11 86 views
1

我找到所有可用的藍牙設備,然後當用戶單擊列表中的某個元素時,我想將我的設備與選定的地址配對,並取消發現。但是,藍牙適配器上的我的cancelDiscovery()調用始終返回false。在android文檔中,它表示適配器必須在STATE_ON中爲cancelDiscovery()返回true。但是,當我調用btAdapter.getState()時,它會返回值12,即STATE_ON。我的代碼在下面,有誰知道還有什麼可能是錯的?cancelDiscovery不斷返回false

我不明白當當前狀態是STATE_ON時,cancelDiscovery()是如何返回false的。文檔說,如果狀態爲STATE_ON,則返回true;對於所有其他值,返回false。

private static final UUID MY_UUID = UUID.randomUUID(); 

private static final int REQUEST_ENABLE_BT = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnScanDevice = (Button) findViewById(R.id.scandevice); 

    stateBluetooth = (TextView) findViewById(R.id.bluetoothstate); 
    startBluetooth(); 

    listDevicesFound = (ListView) findViewById(R.id.devicesfound); 
    btArrayAdapter = new ArrayAdapter<String>(AndroidBluetooth.this, 
      android.R.layout.simple_list_item_1); 
    listDevicesFound.setAdapter(btArrayAdapter); 

    CheckBlueToothState(); 

    btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener); 

    registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

    listDevicesFound.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
     { 
      Log.i("Discovery: ", Integer.toString(myBtAdapter.getState())); 
      boolean success = myBtAdapter.cancelDiscovery(); 
      myBtDevice = btDevicesFound.get(arg2); 
      try { 
       btSocket = myBtDevice.createRfcommSocketToServiceRecord(MY_UUID); 
       iStream = btSocket.getInputStream(); 
       oStream = btSocket.getOutputStream(); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "Bluetooth not available, or insufficient permissions"); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer One"); 
      } 
      CheckBlueToothState(); 
      try { 
       btSocket.connect(); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "Problems arose while attempting to connect."); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer Two"); 
      } 
     } 

    }); 
} 

private void CheckBlueToothState() { 
    if(myBtAdapter == null) { 
     stateBluetooth.setText("Bluetooth NOT supported"); 
    } else { 
     if(myBtAdapter.isEnabled()) { 
      if(myBtAdapter.isDiscovering()) { 
       stateBluetooth.setText("Bluetooth is currently " + 
         "in device discovery process."); 
      } else { 
       stateBluetooth.setText("Bluetooth is Enabled."); 
       btnScanDevice.setEnabled(true); 
      } 
     } else { 
      stateBluetooth.setText("Bluetooth is NOT enabled"); 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 
} 

private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() { 
    public void onClick(View arg0) { 
     btArrayAdapter.clear(); 
     myBtAdapter.startDiscovery(); 
    } 
}; 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == REQUEST_ENABLE_BT) { 
     CheckBlueToothState(); 
    } 
} 

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      btDevicesFound.add(btDevice); 
      btArrayAdapter.add(btDevice.getName() + "\n" + btDevice.getAddress()); 
      btArrayAdapter.notifyDataSetChanged(); 
     }   
    } 
}; 
public static void startBluetooth(){ 
    try { 
     myBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
     myBtAdapter.enable(); 
    } catch (NullPointerException ex) { 
     Log.e("Bluetooth", "Device not available"); 
    } 
} 

public static void stopBluetooth() { 
    myBtAdapter.disable(); 
} 
} 

回答

0

我清理了我的項目,它對任何人在這個線程尋找答案都很好。