public override void OnConnectionStateChange(ABluetooth.BluetoothGatt gatt, ABluetooth.GattStatus status, ABluetooth.ProfileState newState)
 {
     System.Diagnostics.Debug.WriteLine($"ConnectionStateChanged {status}");
     if (newState == ABluetooth.ProfileState.Connected)
     {
         _owner._connected = true;
         _owner._connectedHandle.Set();
         gatt.DiscoverServices();
     }
 }
Beispiel #2
0
 public override void OnConnectionStateChange(ABluetooth.BluetoothGatt gatt, ABluetooth.GattStatus status, ABluetooth.ProfileState newState)
 {
     System.Diagnostics.Debug.WriteLine($"ConnectionStateChanged {status} {newState}");
     _owner.ConnectionStateChanged?.Invoke(_owner, new ConnectionStateEventArgs {
         Status = status, State = newState
     });
     if (newState == ABluetooth.ProfileState.Connected)
     {
         if (!_owner._servicesDiscovered)
         {
             gatt.DiscoverServices();
         }
     }
     else
     {
         _owner.Device.OnGattServerDisconnected();
     }
 }
 public ConnectionStateChangeEventArgs(bt.BluetoothGatt gatt, bt.GattStatus status, bt.ProfileState newState)
 {
     Gatt     = gatt;
     Status   = status;
     NewState = newState;
 }
Beispiel #4
0
        /// <summary>
        /// 获取已配对列表
        /// </summary>
        public List <Util.XamariN.BluetoothDeviceInfo> GetBondedDevices()
        {
            if (this.mBluetoothIsEnabled == false)
            {
                throw new BluetoothException("蓝牙未开启");
            }

            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter; //获取蓝牙

            List <Util.XamariN.BluetoothDeviceInfo> r = new List <Util.XamariN.BluetoothDeviceInfo>();

            foreach (BluetoothDevice item in adapter.BondedDevices)
            {
                string msg = "Name:{0}, MAC:{1}".FormatWith(item.Name, item.Address);
                System.Diagnostics.Debug.WriteLine(msg);

                r.Add
                (
                    new Util.XamariN.BluetoothDeviceInfo
                    (
                        name: item.Name,
                        address: item.Address,
                        bluetoothDeviceType: (int)item.Type,
                        bluetoothBondState: (int)item.BondState
                    )
                );
            }

            // 查看是否蓝牙是否连接到三种设备的一种,以此来判断是否处于连接状态还是打开并没有连接的状态
            Android.Bluetooth.ProfileState a2dp    = adapter.GetProfileConnectionState(ProfileType.A2dp);    // 可操控蓝牙设备,如带播放暂停功能的蓝牙耳机
            Android.Bluetooth.ProfileState headset = adapter.GetProfileConnectionState(ProfileType.Headset); // 蓝牙头戴式耳机,支持语音输入输出
            Android.Bluetooth.ProfileState health  = adapter.GetProfileConnectionState(ProfileType.Health);  // 蓝牙穿戴式设备

            if (a2dp == ProfileState.Connected)
            {
                BluetoothAdapter.DefaultAdapter.GetProfileProxy
                (
                    context: mAppActivity.ApplicationContext,
                    listener: this,
                    profile: ProfileType.A2dp
                );
            }

            if (headset == ProfileState.Connected)
            {
                BluetoothAdapter.DefaultAdapter.GetProfileProxy
                (
                    context: mAppActivity.ApplicationContext,
                    listener: this,
                    profile: ProfileType.Headset
                );
            }

            if (health == ProfileState.Connected)
            {
                BluetoothAdapter.DefaultAdapter.GetProfileProxy
                (
                    context: mAppActivity.ApplicationContext,
                    listener: this,
                    profile: ProfileType.Health
                );
            }

            return(r);
        }
        public override void OnConnectionStateChange(bt.BluetoothGatt gatt, [GeneratedEnum] bt.GattStatus status, [GeneratedEnum] bt.ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);

            ConnectionStateChange?.Invoke(this, new ConnectionStateChangeEventArgs(gatt, status, newState));

            if (!_servicesDiscovered)
            {
                gatt.DiscoverServices();
            }
        }