Example #1
0
        /// <summary>
        /// 获取计量点设备信息
        /// </summary>
        /// <param name="cabinetCode">计量点编号</param>
        /// <returns>计量点设备组</returns>
        public BTDevice[] GetDevice(string cabinetCode)
        {
            BTDevice [] devices = null;
            DataTable   dt      = new DataTable();
            ArrayList   param   = new ArrayList();

            param.Add(cabinetCode);
            CoreClientParam ccp = new CoreClientParam();

            ccp.ServerName      = "com.dbComm.DBComm";
            ccp.MethodName      = "query";
            ccp.ServerParams    = new object[] { "WEIGHDEVICE_01.SELECT", param };
            ccp.SourceDataTable = dt;
            this.ExecuteQueryToDataTable(ccp, CoreInvokeType.Internal);
            if (dt.Rows.Count > 0)
            {
                devices = new BTDevice[dt.Rows.Count];
                DataRow dr = null;
                for (int i = 0; i < devices.Length; i++)
                {
                    dr         = dt.Rows[i];
                    devices[i] = ConvertDataRowToDeviceObject(dr);
                }
            }
            return(devices);
        }
Example #2
0
        public void Initialize()
        {
            ListDevices = new ObservableCollection <BTDevice>();

            IBluetoothManager btMan = DependencyService.Get <IBluetoothManager>();
            var listBTDevices       = btMan.GetBondedDevices();

            if (listBTDevices != null && listBTDevices.Count > 0)
            {
                foreach (var device in listBTDevices)
                {
                    Device = new BTDevice
                    {
                        Name    = device.Name,
                        UUID    = device.UUID,
                        Address = device.Address
                    };
                    ListDevices.Add(Device);
                }
            }
            else
            {
                lbl1 = "Bluetoothデバイスがありません。";
            }
        }
Example #3
0
        private void StackPanel_Tapped(object sender, TappedRoutedEventArgs e)
        {
            BTDevice device = BTDevices[this.DevicesGridView.SelectedIndex];
            MainPage mainpg = MainPage.GetCurrent();

            mainpg.ChangeNavigationSelection(typeof(Home));
            this.Frame.Navigate(typeof(Home), device.DeviceInfo);

            Debug.WriteLine($"Selected device: {device.DeviceName} [{device.DeviceId}]");
        }
Example #4
0
        public List <BTDevice> GetBondedDevices()
        {
            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter; //アダプター作成
            var listBondedDevices    = adapter.BondedDevices;           //ペアリング済みデバイスの取得

            if (listBondedDevices != null && listBondedDevices.Count > 0)
            {
                foreach (var device in listBondedDevices)
                {
                    var btDevice = new BTDevice();
                    btDevice.Name    = device.Name;
                    btDevice.UUID    = device.GetUuids().FirstOrDefault().ToString();
                    btDevice.Address = device.Address;
                    result.Add(btDevice);
                }
            }
            return(result);
        }
Example #5
0
        /// <summary>
        /// 将DataRow转换成BTDEVICE
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private BTDevice ConvertDataRowToDeviceObject(DataRow dr)
        {
            BTDevice device = null;

            if (dr != null)
            {
                try
                {
                    device = new BTDevice();

                    device.FS_CODE  = dr["FS_CODE"].ToString();     //设备编码
                    device.FS_NAME  = dr["FS_NAME"].ToString();     //设备名称
                    device.FS_TYPE  = dr["DEVICE_TYPE"].ToString(); //设备类型
                    device.FS_PARAM = dr["FS_PARAM"].ToString();    //设备参数
                    device.FS_DESC  = dr["FS_DESC"].ToString();     //设备描述
                }
                catch (Exception e)
                {
                }
            }

            return(device);
        }
Example #6
0
    // Update is called once per frame
    void Update()
    {
        if (_timeout > 0f)
        {
            _timeout -= Time.deltaTime;
            if (_timeout <= 0f)
            {
                _timeout = 0f;

                switch (_state)
                {
                case States.None:
                    break;

                case States.Scan:
                    BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, name) => {
                        // if your device does not advertise the rssi and manufacturer specific data
                        // then you must use this callback because the next callback only gets called
                        // if you have manufacturer specific data

                        if (!_rssiOnly)
                        {
                            if (name.Contains(DeviceName))
                            {
                                BluetoothLEHardwareInterface.StopScan();

                                BTDevice device = new BTDevice();
                                device.name     = name;
                                device.addr     = address;
                                _btDevices.Add(device);

                                // found a device with the name we want
                                // this example does not deal with finding more than one
                                _deviceAddress = address;
                            }
                        }
                    }, (address, name, rssi, bytes) => {
                        // use this one if the device responses with manufacturer specific data and the rssi

                        if (name.Contains(DeviceName))
                        {
                            if (_rssiOnly)
                            {
                                _rssi = rssi;
                            }
                            else
                            {
                                BluetoothLEHardwareInterface.StopScan();

                                // found a device with the name we want
                                // this example does not deal with finding more than one
                                _deviceAddress = address;
                                SetState(States.Connect, 0.5f);
                            }
                        }
                    }, _rssiOnly);     // this last setting allows RFduino to send RSSI without having manufacturer data

                    if (_rssiOnly)
                    {
                        SetState(States.ScanRSSI, 0.5f);
                    }
                    break;

                case States.ScanRSSI:
                    break;

                case States.Connect:
                    // set these flags
                    _foundSubscribeID = false;
                    _foundWriteID     = false;

                    // note that the first parameter is the address, not the name. I have not fixed this because
                    // of backwards compatiblity.
                    // also note that I am note using the first 2 callbacks. If you are not looking for specific characteristics you can use one of
                    // the first 2, but keep in mind that the device will enumerate everything and so you will want to have a timeout
                    // large enough that it will be finished enumerating before you try to subscribe or do any other operations.
                    BluetoothLEHardwareInterface.ConnectToPeripheral(_deviceAddress, null, null, (address, serviceUUID, characteristicUUID) => {
                        if (string.Equals(serviceUUID, ServiceUUID, System.StringComparison.OrdinalIgnoreCase))
                        {
                            _foundSubscribeID = _foundSubscribeID || (string.Equals(characteristicUUID, SubscribeCharacteristic, System.StringComparison.OrdinalIgnoreCase));
                            _foundWriteID     = _foundWriteID || (string.Equals(characteristicUUID, WriteCharacteristic, System.StringComparison.OrdinalIgnoreCase));

                            // if we have found both characteristics that we are waiting for
                            // set the state. make sure there is enough timeout that if the
                            // device is still enumerating other characteristics it finishes
                            // before we try to subscribe
                            if (_foundSubscribeID && _foundWriteID)
                            {
                                _connected = true;
                                SetState(States.Subscribe, 2f);
                            }
                        }
                    });
                    break;

                case States.Subscribe:
                    BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_deviceAddress, ServiceUUID, SubscribeCharacteristic, null, (address, characteristicUUID, bytes) => {
                        // we don't have a great way to set the state other than waiting until we actually got
                        // some data back. For this demo with the rfduino that means pressing the button
                        // on the rfduino at least once before the GUI will update.
                        _state = States.None;

                        // we received some data from the device
                        _dataBytes = bytes;
                    });
                    break;

                case States.Unsubscribe:
                    BluetoothLEHardwareInterface.UnSubscribeCharacteristic(_deviceAddress, ServiceUUID, SubscribeCharacteristic, null);
                    SetState(States.Disconnect, 4f);
                    break;

                case States.Disconnect:
                    if (_connected)
                    {
                        BluetoothLEHardwareInterface.DisconnectPeripheral(_deviceAddress, (address) => {
                            BluetoothLEHardwareInterface.DeInitialize(() => {
                                _connected = false;
                                _state     = States.None;
                            });
                        });
                    }
                    else
                    {
                        BluetoothLEHardwareInterface.DeInitialize(() => {
                            _state = States.None;
                        });
                    }
                    break;
                }
            }
        }
    }