internal BluetoothAdapter(Android.Bluetooth.BluetoothAdapter adapter)
 {
     InternalDevice = adapter;
     MACAddress.TryParse(InternalDevice.Address, out MACAddress address);
     Address        = address;
     DeviceProvider = new BluetoothDeviceProvider(this);
 }
        internal BluetoothAdapter(Android.Bluetooth.BluetoothAdapter adapter)
        {
            _adapter = adapter;
            IntentFilter filter = new IntentFilter();

            filter.AddAction(Android.Bluetooth.BluetoothDevice.ActionNameChanged);
            _deviceReceiver = new BluetoothDeviceReceiver(this);
            Android.App.Application.Context.RegisterReceiver(_deviceReceiver, filter);
        }
Beispiel #3
0
 /// <summary>
 /// 打印测试
 /// </summary>
 /// <param name="activity"></param>
 public BluetoothPrinter(Android.App.Activity activity)
 {
     this.activity = activity;
     //获得本地的蓝牙适配器
     localAdapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
     //打开蓝牙设备
     if (!localAdapter.IsEnabled)
     {
         Android.Content.Intent enableIntent = new Android.Content.Intent(Android.Bluetooth.BluetoothAdapter.ActionRequestEnable);
         activity.StartActivityForResult(enableIntent, 1);
     }
     //静默打开
     if (!localAdapter.IsEnabled)
     {
         localAdapter.Enable();
     }
     if (!localAdapter.IsEnabled)//用户拒绝打开或系统限制权限
     {
         Android.Widget.Toast.MakeText(activity, "蓝牙未打开", Android.Widget.ToastLength.Short).Show();
         return;
     }
     //获得已配对的设备列表
     bondedDevices = new List<Android.Bluetooth.BluetoothDevice>(localAdapter.BondedDevices);
     if (bondedDevices.Count <= 0)
     {
         Android.Widget.Toast.MakeText(activity, "未找到已配对的蓝牙设备", Android.Widget.ToastLength.Short).Show();
         return;
     }
     string[] items = new string[bondedDevices.Count];
     //查看本地已设置的printer名称
     string ls_localPrinterName = baseclass.MyConfig.of_GetMySysSet("printer", "name");
     if (!string.IsNullOrEmpty(ls_localPrinterName))
     {
         for (int j = 0; j < bondedDevices.Count; j++)
         {
             if(ls_localPrinterName== bondedDevices[j].Name)
             {
                 as_BluetoothName = ls_localPrinterName;
                 System.Threading.Thread thr2 = new System.Threading.Thread(new System.Threading.ThreadStart(Printer));
                 thr2.Start(activity);
                 return;
             }
         }
     }
     //弹窗选择
     for (int i = 0; i < bondedDevices.Count; i++)
     {
         items[i] = bondedDevices[i].Name;
     }
     Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(activity);
     builder.SetTitle("请选择打印设备:");
     builder.SetItems(items, new System.EventHandler<Android.Content.DialogClickEventArgs>(items_click));
     builder.SetNegativeButton("取消", delegate { return; });
     builder.Show();
 }
Beispiel #4
0
 public BluetoothPrinter(Android.App.Activity activity, PrinterType type, string Number)
 {
     this.as_Number = Number;
     this.printerType = type;
     this.activity = activity;
     //获得本地的蓝牙适配器
     localAdapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
     if (string.IsNullOrEmpty(Number))
     {
         Android.Widget.Toast.MakeText(activity, "传入的单号为空,打印失败", Android.Widget.ToastLength.Short).Show();
         return;
     }
     //打开蓝牙设备
     if (!localAdapter.IsEnabled)
     {
         Android.Content.Intent enableIntent = new Android.Content.Intent(Android.Bluetooth.BluetoothAdapter.ActionRequestEnable);
         activity.StartActivityForResult(enableIntent, 1);
     }
     //静默打开
     if (!localAdapter.IsEnabled)
     {
         localAdapter.Enable();
     }
     if (!localAdapter.IsEnabled)//用户拒绝打开或系统限制权限
     {
         Android.Widget.Toast.MakeText(activity, "蓝牙未打开", Android.Widget.ToastLength.Short).Show();
         return;
     }
     //获得已配对的设备列表
     bondedDevices = new List<Android.Bluetooth.BluetoothDevice>(localAdapter.BondedDevices);
     if (bondedDevices.Count <= 0)
     {
         Android.Widget.Toast.MakeText(activity, "未找到已配对的蓝牙设备", Android.Widget.ToastLength.Short).Show();
         return;
     }
     string[] items = new string[bondedDevices.Count];
     for (int i = 0; i < bondedDevices.Count; i++)
     {
         items[i] = bondedDevices[i].Name;
     }
     Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(activity);
     builder.SetTitle("请选择打印设备:");
     builder.SetItems(items, new System.EventHandler<Android.Content.DialogClickEventArgs>(items_click));
     builder.SetNegativeButton("取消", delegate { return; });
     builder.Show();
 }
Beispiel #5
0
 public bool IsBluetoothEnabled()
 {
     Android.Bluetooth.BluetoothAdapter mBluetoothAdapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
     return(mBluetoothAdapter.IsEnabled);
 }
        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;
        }
Beispiel #7
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            try
            {
                base.OnCreate(savedInstanceState);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);

                string file_data_remember;

                btn_auth_form = FindViewById <Button>(Resource.Id.btn_auth_form);

                string[] permissions = { Manifest.Permission.AccessFineLocation, Manifest.Permission.WriteExternalStorage, Manifest.Permission.Camera, Manifest.Permission.ReadPhoneState, Manifest.Permission.Vibrate, Manifest.Permission.AccessNetworkState,
                                         Manifest.Permission.Bluetooth,          Manifest.Permission.BluetoothAdmin,       Manifest.Permission.Internet };

                Dexter.WithActivity(this).WithPermissions(permissions).WithListener(new CompositeMultiplePermissionsListener(new SamplePermissionListener(this))).Check();
                CrossSettings.Current.AddOrUpdateValue("id", "E353DA5A-07C9-4939-97ED-0CD7CF7B2A7A");


                //Android ID
                //String m_androidId = Android.Provider.Settings.Secure.GetString(ContentResolver, Android.Provider.Settings.Secure.AndroidId);

                //WLAN MAC Address
                //Android.Net.Wifi.WifiManager m_wm = (Android.Net.Wifi.WifiManager)GetSystemService(Android.Content.Context.WifiService);
                //String m_wlanMacAdd = m_wm.ConnectionInfo.MacAddress;

                //Blue-tooth Address
                //Android.Bluetooth.BluetoothAdapter m_BluetoothAdapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
                //String m_bluetoothAdd = m_BluetoothAdapter.Address;

                //if (CrossSettings.Current.GetValueOrDefault("id", "") == "")
                //{
                //    try
                //    {
                //        var box = GetRandomBox();
                //        if (box.Result.Status == "0")
                //        {
                //            CrossSettings.Current.AddOrUpdateValue("id", box.Result.ResponseData.BoxId);
                //            CrossSettings.Current.AddOrUpdateValue("namebox", box.Result.ResponseData.Name);
                //        }
                //        else
                //            Toast.MakeText(this, "" + box.Result.Message, ToastLength.Long).Show();
                //    }
                //    catch(Exception ex)
                //    {
                //        Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show();
                //    }
                //}

                // Переход к форме авторизация
                btn_auth_form.Click += async(s, e) =>
                {
                    Android.Telephony.TelephonyManager mTelephonyMgr;
                    // Android.Telephony.TelephonyManager mTelephonyMgr = (Android.Telephony.TelephonyManager)GetSystemService(Android.Content.Context.TelephonyService);
                    //Telephone Number
                    mTelephonyMgr = (Android.Telephony.TelephonyManager)GetSystemService(TelephonyService);
                    /*var PhoneNumber = mTelephonyMgr.Line1Number*/;

                    if (mTelephonyMgr.DeviceId != null)
                    {
                        //IMEI number
                        StaticBox.DeviceId = mTelephonyMgr.DeviceId;
                    }
                    else if (Android.Provider.Settings.Secure.GetString(ContentResolver, Android.Provider.Settings.Secure.AndroidId) != null)
                    {
                        //Android ID
                        StaticBox.DeviceId = Android.Provider.Settings.Secure.GetString(ContentResolver, Android.Provider.Settings.Secure.AndroidId);
                    }
                    else
                    {
                        //Hash code
                        //StaticBox.DeviceId = "35" + (Build.Board.Length % 10) + (Build.Brand.Length % 10) + (Build.CpuAbi.Length % 10) + (Build.Device.Length % 10) + (Build.Manufacturer.Length % 10) + (Build.Model.Length % 10) + (Build.Product.Length % 10);

                        //Blue-tooth Address
                        Android.Bluetooth.BluetoothAdapter m_BluetoothAdapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
                        string m_bluetoothAdd = m_BluetoothAdapter.Address;
                        StaticBox.DeviceId = m_bluetoothAdd;
                    }

                    await RegisterBox();
                };
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show();
            }
        }