Code example for BluetoothAdapter

Methods: cancelDiscovery, setName

0
        } 
     
     
        public void outData() { 
            // Cancel discovery because it's costly and we're about to connect 
            mBtAdapter.cancelDiscovery();
             
 
            // Set result and finish this Activity 
            if (discovered_devices.size() != 0){
            setResult(Activity.RESULT_OK);}
            else { 
            	setResult(Activity.RESULT_CANCELED);
            } 
        } 
         
     
     
 
    @Override 
    protected void onDestroy() { 
        super.onDestroy(); 
 
        // Make sure we're not doing discovery anymore 
        if (mBtAdapter != null) {
            mBtAdapter.cancelDiscovery();
        } 
 
        // Unregister broadcast listeners 
        this.unregisterReceiver(mReceiver);
    } 
     
    private void doDiscovery() { 
        if (D) Log.d(TAG, "doDiscovery - SetUp");
 
         
        // If we're already discovering, stop it 
        if (mBtAdapter.isDiscovering()) {
            mBtAdapter.cancelDiscovery();
        } 
 
        // Request discover from BluetoothAdapter 
        mBtAdapter.startDiscovery();
    } 
     
 // The BroadcastReceiver that listens for discovered devices and 
    // changes the title when discovery is finished 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
 
            // When discovery finds a device 
            if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
                // Get the BluetoothDevice object from the Intent 
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
                // If it's already paired, skip it, because it's been listed already 
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
                	String dev_name = device.getName(); 
                    if(dev_name.substring(0) == "C"){ 
                    	cluster_id = dev_name.substring(0,c_id_length);   
                	    boolean b = mBtAdapter.setName(cluster_id); 
                	    if (b){ 
                	    	BTPrefs = getSharedPreferences(data_strings,0); 
                	        SharedPreferences.Editor editor = BTPrefs.edit(); 
                	    	editor.putString("C_ID",cluster_id); 
                	    	editor.commit();