Exemplo n.º 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_set_characteristic_value);

            gatt           = SingleGattService.GetInstance().CurGatt;
            service        = SingleGattService.GetInstance().CurService;
            characteristic = SingleGattService.GetInstance().CurCharacteristic;
            callBack       = SingleGattService.GetInstance().CurCallback;

            back           = FindViewById <Button>(Resource.Id.settings_back);
            setValue       = FindViewById <Button>(Resource.Id.settings_set_value);
            registNotify   = FindViewById <Button>(Resource.Id.settings_regist_char_notify);
            unregistNotify = FindViewById <Button>(Resource.Id.settings_unregist_char_notify);
            valueHex       = FindViewById <TextView>(Resource.Id.settings_value_hex);
            valueStr       = FindViewById <TextView>(Resource.Id.settings_value_str);
            valueInput     = FindViewById <EditText>(Resource.Id.settings_value_input);

            back.SetOnClickListener(this);
            setValue.SetOnClickListener(this);
            registNotify.SetOnClickListener(this);
            unregistNotify.SetOnClickListener(this);

            InitView();
        }
        private void StepintoService()
        {
            BluetoothGattService service = services[adapter.GetChooseItem()];

            SingleGattService.GetInstance().CurService = service;
            Intent intent = new Intent();

            intent.SetClass(this, typeof(CharacteristicsActivity));
            StartActivity(intent);
        }
Exemplo n.º 3
0
        //BLEScanCallBack.IDeviceFound

        /*public void OnBleDeviceFound(BluetoothDevice device, int rssi)
         * {
         *  if (device == null ||
         *      device.Name == null || device.Name.Equals("") ||
         *      device.Address == null || device.Address.Equals(""))
         *  {
         *      Log.Error("dana.ye->xamarin->bt", "cur Device Name or Address is null ");
         *      return;
         *  }
         *
         *  if (searchedDevices.Contains(device)) {
         *      Log.Error("dana.ye->xamarin->bt", "cur device has Contained : " + device.Name);
         *  }
         *  searchedDevices.Add(device);
         *  adapter.NotifyDataSetChanged();
         * }*/

        //GattCallBack.IGattCallbackListener
        public void OnGattCallBack()
        {
            Log.Error("dana.ye->xamarin->bt", "进入了成功连接的回调");
            Log.Error("dana.ye->xamarin->bt", "gatt.DiscoverServices()" + gatt.DiscoverServices());
            gatt.DiscoverServices();
            Action act = () =>
            {
                Toast.MakeText(this, "连接成功", ToastLength.Short).Show();
                SingleGattService.GetInstance().CurGatt = gatt;
                Intent intent = new Intent();
                intent.SetClass(this, typeof(ServiceListActivity));
                StartActivity(intent);
            };

            RunOnUiThread(act);
        }
Exemplo n.º 4
0
        public void OnCharacteristicCallback(BluetoothGattCharacteristic characteristic)
        {
            /*byte[] values = characteristic.GetValue();
             * string uuid = characteristic.Uuid + "";
             * string str = "changed characteristic's uuid is : " + characteristic.Uuid + "\r\n";
             * str += "changed characteristic's WriteType is : " + characteristic.WriteType + "\r\n";
             * str += "changed characteristic's GetValue is ";
             * string value = "";
             * foreach (byte b in values)
             * {
             *  str += ": " + b.ToString("X2");
             *  value += b.ToString("X2") + ":";
             * }
             *
             * Action act = () =>
             * {
             *  Toast.MakeText(this, value, ToastLength.Short).Show();
             * };
             * RunOnUiThread(act);
             *
             * Log.Error("dana.ye->xamarin->bt", str);*/

            BluetoothGattCharacteristic c = characteristics[adapter.GetChooseItem()];

            if (c.GetValue() == null)
            {
                Action action = () =>
                {
                    String str = "当前特征值的values为空 , 请换一个特征值查看!";
                    Toast.MakeText(this, str, ToastLength.Short).Show();
                };

                RunOnUiThread(action);
            }
            else
            {
                SingleGattService.GetInstance().CurCharacteristic = c;
                Intent intent = new Intent();
                intent.SetClass(this, typeof(SetCharacteristicValueActivity));
                StartActivity(intent);
            }
        }
Exemplo n.º 5
0
        private void OnDeviceConnect()
        {
            Log.Error("dana.ye->xamarin->bt", "手动点击按钮来停止扫描周围的蓝牙设备");
            btAdapter.CancelDiscovery();

            /*try
             * {
             #pragma warning disable CS0618 // 类型或成员已过时
             *  btAdapter.StopLeScan(bleScanCallback);
             #pragma warning restore CS0618 // 类型或成员已过时
             * }
             * catch (Exception e)
             * {
             *  Log.Error("dana.ye->xamarin->bt", "stopLeScan时出错 : " + e.Message);
             * }*/
            handler.RemoveCallbacks(action);
            if (gatt != null)
            {
                gatt.Disconnect();
                gatt.Close();
            }
            Log.Error("dana.ye->xamarin->bt", "即将进行BLE的设备连接");
            String addr = searchedDevices[adapter.GetChooseItem()].Address;

            SingleGattService.GetInstance().Address = addr;
            BluetoothDevice device        = btAdapter.GetRemoteDevice(addr);
            GattCallBack    mGattCallback = new GattCallBack(this);

            SingleGattService.GetInstance().CurCallback = mGattCallback;
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M)
            {
                Log.Error("dana.ye->xamarin->bt", "SdkInt >= M");
                gatt = device.ConnectGatt(this, false, mGattCallback, BluetoothTransports.Le);
            }
            else
            {
                Log.Error("dana.ye->xamarin->bt", "SdkInt < M");
                gatt = device.ConnectGatt(this, false, mGattCallback);
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_service);
            services = new List <BluetoothGattService>();
            gatt     = SingleGattService.GetInstance().CurGatt;

            serviceList     = FindViewById <GridView>(Resource.Id.service_list);
            back            = FindViewById <Button>(Resource.Id.service_back);
            refreshService  = FindViewById <Button>(Resource.Id.service_refresh);
            stepintoService = FindViewById <Button>(Resource.Id.service_stepinto);

            adapter                         = new ServicesAdapter(this, services);
            serviceList.Adapter             = adapter;
            serviceList.OnItemClickListener = this;
            back.SetOnClickListener(this);
            refreshService.SetOnClickListener(this);
            stepintoService.SetOnClickListener(this);

            RefreshBluetoothDeviceService();
        }
Exemplo n.º 7
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_characteristics);
            characteristics = new List <BluetoothGattCharacteristic>();
            gatt            = SingleGattService.GetInstance().CurGatt;
            service         = SingleGattService.GetInstance().CurService;
            callBack        = SingleGattService.GetInstance().CurCallback;

            characteristicsList = FindViewById <GridView>(Resource.Id.characteristics_list);
            back = FindViewById <Button>(Resource.Id.characteristics_back);
            refreshCharacteristics  = FindViewById <Button>(Resource.Id.characteristics_refresh);
            stepintoCharacteristics = FindViewById <Button>(Resource.Id.characteristics_stepinto);

            adapter = new CharacteristicsAdapter(this, characteristics);
            characteristicsList.Adapter             = adapter;
            characteristicsList.OnItemClickListener = this;
            back.SetOnClickListener(this);
            refreshCharacteristics.SetOnClickListener(this);
            stepintoCharacteristics.SetOnClickListener(this);

            RefreshCharacteristics();
        }