コード例 #1
0
        public override void OnReceive(Context context, Intent intent)
        {
            System.String action = intent.Action;
            if (Android.Bluetooth.BluetoothDevice.ActionFound.Equals(action))
            {
                Android.Bluetooth.BluetoothDevice device =
                    (Android.Bluetooth.BluetoothDevice)intent.GetParcelableExtra(Android.Bluetooth.BluetoothDevice.ExtraDevice);

                var btDevice = new KeyVendor.Models.BluetoothDevice()
                {
                    Name    = device.Name,
                    Address = device.Address
                };

                bool itemAlreadyAdded = false;

                foreach (var item in DeviceList)
                {
                    if (item.Address == btDevice.Address)
                    {
                        itemAlreadyAdded = true;
                        break;
                    }
                }

                if (!itemAlreadyAdded)
                {
                    DeviceList.Add(btDevice);
                }
            }
        }
コード例 #2
0
                public virtual void OnCharacteristicRead(Android.Bluetooth.BluetoothDevice device, int requestId, int offset)
                {
                    CharacteristicReadRequest readRequest = new CharacteristicReadRequest
                    {
                        SourceDevice         = BluetoothManager.BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice((Service.Server as GattServer).BluetoothManager, device),
                        TargetCharacteristic = this,
                        Offset    = offset,
                        RequestId = requestId,
                    };

                    OnRead?.Invoke(this, readRequest);
                    //Service.Server.SendResponse(BluetoothManager.BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice((Service.Server as GattServer).BluetoothManager, device), requestId, null);
                }
コード例 #3
0
                public virtual void OnCharacteristicWrite(Android.Bluetooth.BluetoothDevice droidDevice, int requestId, Android.Bluetooth.BluetoothGattCharacteristic characteristic, bool preparedWrite, bool responseNeeded, int offset, byte[] value)
                {
                    var          device       = BluetoothManager.BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice((Service.Server as GattServer).BluetoothManager, droidDevice);
                    WriteRequest writeRequest = new WriteRequest
                    {
                        Device         = device,
                        Offset         = offset,
                        ResponseNeeded = responseNeeded,
                        RequestId      = requestId,
                        Value          = value
                    };

                    OnWrite?.Invoke(this, writeRequest);
                }
コード例 #4
0
            //private static final String TAG = "SBeaconParser";

            public override Beacon FromScanData(byte[] scanData, int rssi, Android.Bluetooth.BluetoothDevice device)
            {
                int startByte = 2;

                while (startByte <= 5)
                {
                    // "m:2-3=0203,i:2-2,i:7-8,i:14-19,d:10-13,p:9-9"
                    if (((int)scanData[startByte + 3] & 0xff) == 0x03 &&
                        ((int)scanData[startByte + 4] & 0xff) == 0x15)
                    {
                        //BeaconManager.logDebug(TAG, "This is a SBeacon beacon advertisement");
                        // startByte+0 company id (2 bytes)
                        // startByte+2 = 02 (1) byte header
                        // startByte+3 = 0315 (2 bytes) header
                        // startByte+5 = Beacon Type 0x01
                        // startByte+6 = Reserved (1 bytes)
                        // startByte+7 = Security Code (2 bytes) => Major little endian
                        // startByte+9 = Tx Power => Tx Power
                        // startByte+10 = Timestamp (4 bytes) => Minor (2 LSBs) little endian
                        // startByte+14 = Beacon ID (6 bytes) -> UUID little endian
                        int grouping = (scanData[startByte + 8] & 0xff) * 0x100 + (scanData[startByte + 7] & 0xff);
                        int clock    = (scanData[startByte + 13] & 0xff) * 0x1000000 + (scanData[startByte + 12] & 0xff) * 0x10000 + (scanData[startByte + 11] & 0xff) * 0x100 + (scanData[startByte + 10] & 0xff);
                        int txPower  = (int)(sbyte)scanData[startByte + 9];  // this one is signed

                        byte[] beaconId = new byte[6];
                        //Java.Lang.JavaSystem.Arraycopy(scanData, startByte+14, beaconId, 0, 6);
                        Array.Copy(scanData, startByte + 14, beaconId, 0, 6);
                        String        hexString = BytesToHex(beaconId);
                        StringBuilder sb        = new StringBuilder();
                        sb.Append(hexString.Substring(0, 12));
                        String id             = "0x" + sb.ToString();
                        int    beaconTypeCode = (scanData[startByte + 3] & 0xff) * 0x100 + (scanData[startByte + 2] & 0xff);


                        String mac = null;
                        if (device != null)
                        {
                            mac = device.Address;
                        }
                        Beacon beacon = new SBeacon(grouping, id, clock, txPower, rssi, beaconTypeCode, mac);
                        return(beacon);
                    }
                    startByte++;
                }
                return(null);
            }
コード例 #5
0
 private BluetoothDeviceWrapper(Android.Bluetooth.BluetoothDevice bluetoothDevice)
 {
     this.DroidDevice = bluetoothDevice;
     IsFetchingUuids  = false;
     _Receiver        = new Receiver(this);
     ParcelUuid[] uuids = bluetoothDevice.GetUuids();
     if (uuids != null)
     {
         Guid[] guids = new Guid[uuids.Length];
         for (int i = 0; i < uuids.Length; i++)
         {
             Guid guid = Guid.Parse(uuids[i].Uuid.ToString());
             guids[i] = guid;
         }
         LastestFetchedUuids = guids;
     }
 }
コード例 #6
0
        public string GetMACAdress(object device)
        {
            string result = "";

            try
            {
                Android.Bluetooth.BluetoothDevice ble = (Android.Bluetooth.BluetoothDevice)device;

                if (ble != null)
                {
                    result = ble.Address;
                }
            }
            catch (System.Exception ex)
            {
            }

            return(result);
        }
コード例 #7
0
            public override void OnReceive(Context context, Intent intent)
            {
                string action = intent.Action;

                if (action == Android.Bluetooth.BluetoothAdapter.ActionDiscoveryStarted)
                {
                    Application.Context.UnregisterReceiver(this);
                }
                if (action == Android.Bluetooth.BluetoothDevice.ActionFound)
                {
                    Android.Bluetooth.BluetoothDevice droidDevice = intent.GetParcelableExtra(Android.Bluetooth.BluetoothDevice.ExtraDevice) as Android.Bluetooth.BluetoothDevice;
                    var deviceWrapper = BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(RfcommScanner.BluetoothManager, droidDevice);
                    RfcommScanner.Added?.Invoke(RfcommScanner, deviceWrapper);
                }
                if (action == Android.Bluetooth.BluetoothAdapter.ActionDiscoveryFinished)
                {
                    Application.Context.UnregisterReceiver(this);
                    Application.Context.UnregisterReceiver(RfcommScanner.DevicesFoundReceiver);
                    RfcommScanner.Status = BluetoothRfcommScannerState.EnumerationCompleted;
                    RfcommScanner.EnumerationCompleted?.Invoke(RfcommScanner, null);
                }
            }
コード例 #8
0
 internal DeviceInformationPairing(Android.Bluetooth.BluetoothDevice device)
 {
     _device = device;
 }
コード例 #9
0
 private BluetoothDeviceWrapper(Android.Bluetooth.BluetoothDevice bluetoothDevice)
 {
     DroidDevice = bluetoothDevice;
     _Receiver   = new Receiver(this);
     _Services   = new List <RfcommDeviceService>();
 }
コード例 #10
0
            /// <summary>
            /// Will this update the BluetoothDevice locally ?
            /// Not yet! But I think it will be someday
            /// </summary>
            /// <param name="bluetoothManager"></param>
            /// <param name="droidDevice"></param>
            /// <returns></returns>
            public static BluetoothDeviceWrapper GetBluetoothDeviceFromDroidDevice(BluetoothManager bluetoothManager, Android.Bluetooth.BluetoothDevice droidDevice)
            {
                var existDevice = bluetoothManager._KnownBluetoothDevices.GetFromAddress(droidDevice.Address);

                if (existDevice == null)
                {
                    existDevice = new BluetoothDeviceWrapper(droidDevice);
                    bluetoothManager._KnownBluetoothDevices.Add(existDevice);
                }
                return(existDevice);
            }
        public BluetoothPrinterService()
        {
            BluetoothLowEnergy ble = new BluetoothLowEnergy();



            Android.Bluetooth.BluetoothAdapter ba = null;
            try
            {
                ba = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;

                if (null != ba)
                {
                    try
                    {
                        Android.Bluetooth.State state = ba.State;
                    }
                    catch (Exception exc)
                    {
                        Android.Widget.Toast.MakeText
                        (
                            Android.App.Application.Context,
                            "Add bluetooth permission",
                            Android.Widget.ToastLength.Long
                        ).Show();
                    }
                }
            }
            catch (Exception exc)
            {
                string msg = "if this fails - AndroidManifest.xml";
                LoggerService.WriteLine(msg);
            }

            if (ba == null)
            {
                LoggerService.WriteLine("BluetoothAdapter == null");
                return;
            }

            if (!ba.IsEnabled)
            {
                LoggerService.WriteLine("Bluetooth adapter is not enabled.");
            }

            foreach (var bd in ba.BondedDevices)
            {
                LoggerService.WriteLine(bd.Name);
            }
            string device_name = "Qsprinter";

            Android.Bluetooth.BluetoothDevice device =
                (
                    from bd in ba.BondedDevices
                    where bd.Name == device_name
                    select bd
                ).FirstOrDefault();

            if (device == null)
            {
                LoggerService.WriteLine("Bluetooth device not found = " + device_name);
            }

            Android.OS.ParcelUuid[] uuids = device.GetUuids();
            string uuid =
                uuids[0].ToString()
                //Guid.NewGuid().ToString()
            ;

            _socket = device
                      .CreateInsecureRfcommSocketToServiceRecord(Java.Util.UUID.FromString(uuid))
                      //.CreateRfcommSocketToServiceRecord (Java.Util.UUID.FromString (uuid))
            ;


            try
            {
                _socket.Connect();
                if (_socket.IsConnected)
                {
                    LoggerService.WriteLine("Bluetooth socket connected ");
                }
            }
            catch (Exception exc)
            {
//			[BluetoothUtils] isSocketAllowedBySecurityPolicy start : device null
//			[BluetoothSocket] GlobalConfig.GLOBALCONFIG_BT_IT_POLICY_FEATURE = true
//			[BluetoothAdapter] getBluetoothService() called with no BluetoothManagerCallback
//			[BluetoothSocket] connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[78]}
            }

            byte[] data_bytes = System.Text.Encoding.UTF8.GetBytes("Bluetooth created");

            // Write data to the device
            //await
            try
            {
                _socket.OutputStream.WriteAsync(data_bytes, 0, data_bytes.Length);
            }
            catch (Exception exc)
            {
            }

            return;
        }
コード例 #12
0
 internal BluetoothDevice(Android.Bluetooth.BluetoothDevice device)
 {
     InternalDevice = device;
     MACAddress.TryParse(InternalDevice.Address, out MACAddress address);
     Address = address;
 }
コード例 #13
0
 internal BluetoothDevice(Android.Bluetooth.BluetoothDevice device)
 {
     _device = device;
 }