Ejemplo n.º 1
0
            public override void OnReceive(Context context, Intent intent)
            {
                String action = intent.Action;

                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.Equals(action))
                {
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.GetParcelableExtra <BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE);
                    // If it's already paired, skip it, because it's been listed already
                    if (device.GetBondState() != BluetoothDevice.BOND_BONDED)
                    {
                        activity.mNewDevicesArrayAdapter.Add(device.GetName() + "\n" + device.GetAddress());
                    }
                    // When discovery is finished, change the Activity title
                }
                else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.Equals(action))
                {
                    activity.SetProgressBarIndeterminateVisibility(false);
                    activity.SetTitle(R.String.select_device);
                    if (activity.mNewDevicesArrayAdapter.GetCount() == 0)
                    {
                        var noDevices = activity.Resources.GetText(R.String.none_found).ToString();
                        activity.mNewDevicesArrayAdapter.Add(noDevices);
                    }
                }
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Start the ConnectedThread to begin managing a Bluetooth connection </summary>
        /// <param name="socket">  The BluetoothSocket on which the connection was made </param>
        /// <param name="device">  The BluetoothDevice that has been connected </param>
        public void Connected(BluetoothSocket socket, BluetoothDevice device, string socketType)
        {
            lock (this)
            {
                if (D)
                {
                    Log.D(TAG, "connected, Socket Type:" + socketType);
                }

                // Cancel the thread that completed the connection
                if (mConnectThread != null)
                {
                    mConnectThread.Cancel();
                    mConnectThread = null;
                }

                // Cancel any thread currently running a connection
                if (mConnectedThread != null)
                {
                    mConnectedThread.Cancel();
                    mConnectedThread = null;
                }

                // Cancel the accept thread because we only want to connect to one device
                if (mSecureAcceptThread != null)
                {
                    mSecureAcceptThread.Cancel();
                    mSecureAcceptThread = null;
                }
                if (mInsecureAcceptThread != null)
                {
                    mInsecureAcceptThread.Cancel();
                    mInsecureAcceptThread = null;
                }

                // Start the thread to manage the connection and perform transmissions
                mConnectedThread = new ConnectedThread(this, socket, socketType);
                mConnectedThread.Start();

                // Send the name of the connected device back to the UI Activity
                Message msg    = mHandler.ObtainMessage(global::BluetoothChat.BluetoothChat.MESSAGE_DEVICE_NAME);
                Bundle  bundle = new Bundle();
                bundle.PutString(global::BluetoothChat.BluetoothChat.DEVICE_NAME, device.GetName());
                msg.SetData(bundle);
                mHandler.SendMessage(msg);

                State = STATE_CONNECTED;
            }
        }
        /// <summary>
        ///
        /// </summary>
        public virtual void OnLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
        {
            // Get Address.
            string address = device.GetAddress();

            if (string.IsNullOrEmpty(address))
            {
                return;
            }
            Log.i("BluetoothScanner", address);
            //
            int i = deviceList.FindIndex((x) => (x.address == address));

            if (i == -1)
            {
                deviceList.Add(new DeviceInfo {
                    name = device.GetName(), address = address
                });
                if (callback != null)
                {
                    callback.OnDeviceListChanged();
                }
            }
            else
            {
                address = device.GetName();
                if (!string.IsNullOrEmpty(address))
                {
                    deviceList[i].name = address;
                }
                if (callback != null)
                {
                    callback.OnDeviceListChanged();
                }
            }
        }