Android BluetoothDevice - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android BluetoothDevice class.

This code example shows how to use the following methods:getAddress, getName, getAlias
 
    private String mAddress;
 
    BluetoothTriggerPreference(Context context, BluetoothDevice device) {
        super(context);
        mAddress = device.getAddress();
        if (device.getAlias() != null) {
            setTitle(device.getAlias());
        } else { 
            setTitle(device.getName());
        } 
    } 
 
    BluetoothTriggerPreference(Context context, String name, String address) {
        super(context);
        mAddress = address;
        setTitle(name);
    } 
 
    public String getAddress() {
8
CodeRank
This code example shows how to use the following methods:getAddress, getBondState, getName
		public void onReceive(final Context pContext, final Intent pIntent) { 
			final String action = pIntent.getAction(); 
 
			if (action.equals(BluetoothDevice.ACTION_FOUND)) { 
				final BluetoothDevice device = pIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
				if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
					BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
				} 
			} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { 
				BluetoothListDevicesActivity.this.setProgressBarIndeterminateVisibility(false); 
				BluetoothListDevicesActivity.this.setTitle("Select a device to connect..."); 
				if (BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.getCount() == 0) { 
					BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.add("No devices found!"); 
				} 
			} 
		} 
	}; 
 
 
	// =========================================================== 
7
CodeRank
This code example shows how to use the following methods:createRfcommSocketToServiceRecord, getAddress, getName
            Log.i(TAG, getId() + ":begin mConnectThread");
            BluetoothSocket tmp = null;
            // Always cancel discovery because it will slow down a connection 
 
            try { 
                tmp = mmDevice.createRfcommSocketToServiceRecord(BT_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Socket create() failed", e);
                return; 
            } 
            mmSocket = tmp;
            // Make a connection to the BluetoothSocket 
            if (mAdapter.isEnabled()) mAdapter.cancelDiscovery();
            try { 
                // This is a blocking call and will only return on a 
                // successful connection or an exception 
                if (D)  Log.d(TAG, getId() + ":Connecting to socket...");
                mmSocket.connect();
                if (D) Log.d(TAG, "connected with remote device: "
                        + mmDevice.getName() + " at address " + mmDevice.getAddress());

7
CodeRank
This code example shows how to use the following methods:createRfcommSocketToServiceRecord
            // Get a BluetoothSocket for a connection with the 
            // given BluetoothDevice 
            try { 
//                Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
//                tmp = (BluetoothSocket) m.invoke(device, 1); 
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "Socket Type: " + mSocketType + " create() failed", e);
            } 
            mmSocket = tmp;
        } 
 
        public void run() { 
            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
            setName("ConnectThread" + mSocketType);
 
            // Always cancel discovery because it will slow down a connection 
            mAdapter.cancelDiscovery();
 
6
CodeRank
This code example shows how to use the following methods:createInsecureRfcommSocketToServiceRecord, createRfcommSocketToServiceRecord
 
            // Get a BluetoothSocket for a connection with the given BluetoothDevice 
            try { 
                if (secure) {
                    // tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE); 
                    tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
                } else { 
                    tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
                } 
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
            } 
            mmSocket = tmp;
        } 
 
        public void run() { 
            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
            setName("ConnectThread" + mSocketType);
 
            // Always cancel discovery because it will slow down a connection 
6
CodeRank
This code example shows how to use the following methods:getAddress, getName
			String action = intent.getAction(); 
			 
			if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
				Log.v(SSC,"Bluetooth discovering..."); 
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
				bluetoothNeighbors.add(device.getName() + ";" + device.getAddress()); 
			} else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
				Log.v(SSC,"Bluetooth discovery finished."); 
				ba.disable(); 
			} else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { 
				Log.v(SSC,"Bluetooth discovery starting..."); 
				bluetoothNeighbors.clear(); 
			} 
		} 
    }; 
     
    private final SensorEventListener sl = new SensorEventListener() {
		 
		public void onSensorChanged(SensorEvent event) { 
			sensorValues = event.values;			 
6
CodeRank
This code example shows how to use the following methods:createInsecureRfcommSocketToServiceRecord, createRfcommSocketToServiceRecord
 
            // Get a BluetoothSocket for a connection with the given BluetoothDevice 
            try { 
                if (secure) {
                    // tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE); 
                    tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
                } else { 
                    tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
                } 
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
            } 
            mmSocket = tmp;
        } 
 
        public void run() { 
            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
            setName("ConnectThread" + mSocketType);
 
            // Always cancel discovery because it will slow down a connection 
6
CodeRank
This code example shows how to use the following methods:equals
 
    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
 
        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
                ? mService.getConnectionState(device)
                : BluetoothProfile.STATE_DISCONNECTED;
    } 
 
    public boolean isPreferred(BluetoothDevice device) {
        if (mService == null) return false;
        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
    } 
 
    public int getPreferred(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
        return mService.getPriority(device);
    } 
 
6
CodeRank
This code example shows how to use the following methods:getAddress, getName
	private BluetoothDevice _device;
	private BluetoothSocket _socket;
	private int _id;
	 
	public BluetoothPlayer(BluetoothDevice device) {
		super(device.getAddress(), device.getName());
		_device = device;
	} 
 
	public BluetoothDevice getDevice() {
		return _device;
	} 
	 
	public void setSocket(BluetoothSocket socket){
		_socket = socket;
	} 
	 
	public BluetoothSocket getSocket(){
		return _socket;
	} 
6
CodeRank
This code example shows how to use the following methods:createInsecureRfcommSocketToServiceRecord
		 
		@Override 
		public void connect() throws IOException { 
		    if (mmSocket == null) {
    			BluetoothDevice device = mmBluetoothAdapter.getRemoteDevice(mmMac);
    			mmSocket = device.createInsecureRfcommSocketToServiceRecord(mmServiceUuid);
    			mmSocket.connect();
		    } 
		} 
		 
		@Override 
		public InputStream getInputStream() throws IOException {
			return mmSocket.getInputStream();
		} 
		 
		@Override 
		public OutputStream getOutputStream() throws IOException {
			return mmSocket.getOutputStream();
		} 
		 
6
CodeRank

Related Android BluetoothDevice Questions & Answers:

See Code Examples for Android BluetoothDevice Methods: