Search in sources :

Example 51 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project android_frameworks_base by AOSPA.

the class ScanResultTest method testScanResultParceling.

/**
     * Test read and write parcel of ScanResult
     */
@SmallTest
public void testScanResultParceling() {
    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("01:02:03:04:05:06");
    byte[] scanRecord = new byte[] { 1, 2, 3 };
    int rssi = -10;
    long timestampMicros = 10000L;
    ScanResult result = new ScanResult(device, ScanRecord.parseFromBytes(scanRecord), rssi, timestampMicros);
    Parcel parcel = Parcel.obtain();
    result.writeToParcel(parcel, 0);
    // Need to reset parcel data position to the beginning.
    parcel.setDataPosition(0);
    ScanResult resultFromParcel = ScanResult.CREATOR.createFromParcel(parcel);
    assertEquals(result, resultFromParcel);
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) Parcel(android.os.Parcel) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 52 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project android_frameworks_base by AOSPA.

the class ScanFilterTest method setUp.

@Override
protected void setUp() throws Exception {
    byte[] scanRecord = new byte[] { // advertising flags
    0x02, // advertising flags
    0x01, // advertising flags
    0x1a, // 16 bit service uuids
    0x05, // 16 bit service uuids
    0x02, // 16 bit service uuids
    0x0b, // 16 bit service uuids
    0x11, // 16 bit service uuids
    0x0a, // 16 bit service uuids
    0x11, // setName
    0x04, // setName
    0x09, // setName
    0x50, // setName
    0x65, // setName
    0x64, // tx power level
    0x02, // tx power level
    0x0A, // tx power level
    (byte) 0xec, // service data
    0x05, // service data
    0x16, // service data
    0x0b, // service data
    0x11, // service data
    0x50, // service data
    0x64, // manufacturer specific data
    0x05, // manufacturer specific data
    (byte) 0xff, // manufacturer specific data
    (byte) 0xe0, // manufacturer specific data
    0x00, // manufacturer specific data
    0x02, // manufacturer specific data
    0x15, // an unknown data type won't cause trouble
    0x03, // an unknown data type won't cause trouble
    0x50, // an unknown data type won't cause trouble
    0x01, // an unknown data type won't cause trouble
    0x02 };
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice device = adapter.getRemoteDevice(DEVICE_MAC);
    mScanResult = new ScanResult(device, ScanRecord.parseFromBytes(scanRecord), -10, 1397545200000000L);
    mFilterBuilder = new ScanFilter.Builder();
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) ScanFilter(android.bluetooth.le.ScanFilter) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 53 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project LEGO-MINDSTORMS-MINDdroid by NXT.

the class BTCommunicator method createNXTconnection.

/**
     * Create a bluetooth connection with SerialPortServiceClass_UUID
     * @see <a href=
     *      "http://lejos.sourceforge.net/forum/viewtopic.php?t=1991&highlight=android"
     *      />
     * On error the method either sends a message to it's owner or creates an exception in the
     * case of no message handler.
     */
public void createNXTconnection() throws IOException {
    try {
        BluetoothSocket nxtBTSocketTemporary;
        BluetoothDevice nxtDevice = null;
        nxtDevice = btAdapter.getRemoteDevice(mMACaddress);
        if (nxtDevice == null) {
            if (uiHandler == null)
                throw new IOException();
            else {
                sendToast(mResources.getString(R.string.no_paired_nxt));
                sendState(STATE_CONNECTERROR);
                return;
            }
        }
        nxtBTSocketTemporary = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);
        try {
            nxtBTSocketTemporary.connect();
        } catch (IOException e) {
            if (myOwner.isPairing()) {
                if (uiHandler != null) {
                    sendToast(mResources.getString(R.string.pairing_message));
                    sendState(STATE_CONNECTERROR_PAIRING);
                } else
                    throw e;
                return;
            }
            // try another method for connection, this should work on the HTC desire, credits to Michael Biermann
            try {
                Method mMethod = nxtDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
                nxtBTSocketTemporary = (BluetoothSocket) mMethod.invoke(nxtDevice, Integer.valueOf(1));
                nxtBTSocketTemporary.connect();
            } catch (Exception e1) {
                if (uiHandler == null)
                    throw new IOException();
                else
                    sendState(STATE_CONNECTERROR);
                return;
            }
        }
        nxtBTsocket = nxtBTSocketTemporary;
        nxtInputStream = nxtBTsocket.getInputStream();
        nxtOutputStream = nxtBTsocket.getOutputStream();
        connected = true;
    } catch (IOException e) {
        if (uiHandler == null)
            throw e;
        else {
            if (myOwner.isPairing())
                sendToast(mResources.getString(R.string.pairing_message));
            sendState(STATE_CONNECTERROR);
            return;
        }
    }
    // everything was OK
    if (uiHandler != null)
        sendState(STATE_CONNECTED);
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) IOException(java.io.IOException) Method(java.lang.reflect.Method) BluetoothSocket(android.bluetooth.BluetoothSocket) IOException(java.io.IOException)

Example 54 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project LEGO-MINDSTORMS-MINDdroid by NXT.

the class DeviceListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Setup the window
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);
    // Set result CANCELED incase the user backs out
    setResult(Activity.RESULT_CANCELED);
    // Initialize the button to perform device discovery
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });
    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices
    mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    // Find and set up the ListView for paired devices
    ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedListView.setAdapter(mPairedDevicesArrayAdapter);
    pairedListView.setOnItemClickListener(mDeviceClickListener);
    // Find and set up the ListView for newly discovered devices
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);
    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);
    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);
    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    // Get a set of currently paired devices
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
    // If there are paired devices, add each one to the ArrayAdapter
    boolean legoDevicesFound = false;
    if (pairedDevices.size() > 0) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice device : pairedDevices) {
            // only add LEGO devices
            if (device.getAddress().startsWith(BTCommunicator.OUI_LEGO)) {
                legoDevicesFound = true;
                mPairedDevicesArrayAdapter.add(device.getName() + "-" + device.getAddress());
            }
        }
    }
    if (legoDevicesFound == false) {
        String noDevices = getResources().getText(R.string.none_paired).toString();
        mPairedDevicesArrayAdapter.add(noDevices);
    }
}
Also used : IntentFilter(android.content.IntentFilter) ListView(android.widget.ListView) BluetoothDevice(android.bluetooth.BluetoothDevice) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView)

Example 55 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project MWM-for-Android-Gen1 by MetaWatchOpenProjects.

the class MetaWatchService method connect.

void connect(Context context) {
    try {
        Log.d(MetaWatch.TAG, "Remote device address: " + Preferences.watchMacAddress);
        if (Preferences.watchMacAddress.equals(""))
            loadPreferences(context);
        BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(Preferences.watchMacAddress);
        if (Preferences.skipSDP) {
            Method method = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
            bluetoothSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);
        } else {
            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
            bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
        }
        //Log.d(MetaWatch.TAG, "got Bluetooth socket");
        //if (bluetoothSocket == null)
        //Log.d(MetaWatch.TAG, "Bluetooth socket is null");
        bluetoothSocket.connect();
        inputStream = bluetoothSocket.getInputStream();
        outputStream = bluetoothSocket.getOutputStream();
        connectionState = ConnectionState.CONNECTED;
        updateNotification(true);
        Protocol.sendRtcNow(context);
        Protocol.getDeviceType();
    } catch (IOException ioexception) {
        Log.d(MetaWatch.TAG, ioexception.toString());
        sendToast(ioexception.toString());
    } catch (SecurityException e) {
        Log.d(MetaWatch.TAG, e.toString());
    } catch (NoSuchMethodException e) {
        Log.d(MetaWatch.TAG, e.toString());
    } catch (IllegalArgumentException e) {
        Log.d(MetaWatch.TAG, e.toString());
    } catch (IllegalAccessException e) {
        Log.d(MetaWatch.TAG, e.toString());
    } catch (InvocationTargetException e) {
        Log.d(MetaWatch.TAG, e.toString());
    }
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) Method(java.lang.reflect.Method) IOException(java.io.IOException) UUID(java.util.UUID) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BluetoothDevice (android.bluetooth.BluetoothDevice)101 Intent (android.content.Intent)13 BluetoothAdapter (android.bluetooth.BluetoothAdapter)12 CachedBluetoothDevice (com.android.settingslib.bluetooth.CachedBluetoothDevice)11 ParcelUuid (android.os.ParcelUuid)9 RemoteException (android.os.RemoteException)9 ScanFilter (android.bluetooth.le.ScanFilter)5 Parcel (android.os.Parcel)5 SmallTest (android.test.suitebuilder.annotation.SmallTest)5 IOException (java.io.IOException)5 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)5 IntentFilter (android.content.IntentFilter)4 MidiDeviceInfo (android.media.midi.MidiDeviceInfo)4 View (android.view.View)4 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2