예제 #1
0
        public async Task <bool> StartBroadcastingBle(Context context, Shoot ev)
        {
            //ask to turn on bluetooth:
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;

            var allprefs = context.GetSharedPreferences(WhiteLabelConfig.BUILD_VARIANT.ToLower(), FileCreationMode.Private);
            //var prefs = allprefs.Edit();
            var useble = allprefs.GetBoolean("ble-" + ev.id, true);

            //prefs.PutInt("ble_enable", currentversion);
            //prefs.Apply();
            if (!useble)
            {
                return(false);
            }

            if (mBluetoothAdapter.State == State.Off)
            {
                //ask to turn on:
                TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
                var dialog = new Android.Support.V7.App.AlertDialog.Builder(context);
                dialog.SetTitle(Resource.String.enableble);
                dialog.SetMessage(Resource.String.blemessage);
                dialog.SetPositiveButton(Android.Resource.String.Yes, (o, e) =>
                {
                    tcs.SetResult(true);
                });

                dialog.SetNegativeButton(Android.Resource.String.No, (o, e) =>
                {
                    tcs.SetResult(false);
                });

                dialog.Show();

                var result = await tcs.Task;
                if (result)
                {
                    allprefs.Edit().PutBoolean("ble-" + ev.id, true).Apply();

                    //turn on ble
                    mBluetoothAdapter.Enable();
                    //need to wait for ble event saying its started here:
                    await Task.WhenAny(Task.Delay(3000), Task.Factory.StartNew(() =>
                    {
                        while (mBluetoothAdapter.State != State.On)
                        {
                            Task.Yield();
                        }
                    }));
                }
                else
                {
                    allprefs.Edit().PutBoolean("ble-" + ev.id, false).Apply();
                    return(false);
                }
            }

            var compat = BeaconTransmitter.CheckTransmissionSupported(context);

            if (compat == BeaconTransmitter.Supported)
            {
                try
                {
                    byte[] urlBytes = UrlBeaconUrlCompressor.Compress("https://" + WhiteLabelConfig.BEACONHOST + "/b/");// 91071");//+ev.offlinecode);
                    //HACK: remove line!
                    //ev.offlinecode = "91071";
                    List <byte> bytes = new List <byte>(urlBytes);
                    bytes.AddRange(Encoding.ASCII.GetBytes(ev.offlinecode));

                    urlBytes = bytes.ToArray();

                    Identifier        encodedUrlIdentifier = Identifier.FromBytes(urlBytes, 0, urlBytes.Length, false);
                    List <Identifier> identifiers          = new List <Identifier>();
                    identifiers.Add(encodedUrlIdentifier);
                    beacon = new Beacon.Builder()
                             .SetIdentifiers(identifiers)
                             .SetServiceUuid(0xfeaa)
                             .SetManufacturer(0x0118)
                             .SetBeaconTypeCode(0x10)
                             .SetTxPower(-59)
                             .Build();
                    BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(BeaconParser.EddystoneUrlLayout);

                    beaconTransmitter = new BeaconTransmitter(context, beaconParser);

                    beaconTransmitter.StartAdvertising(beacon, this);
                }
                catch (MalformedURLException)
                {
                    Log.Debug("BOOTLEGGER", "That URL cannot be parsed");
                }
                return(true);
            }
            else
            {
                //not supported
                return(false);
            }
        }
        /// <summary>
        /// 端末がBLEの発信に対応しているかどうかをチェックする処理
        /// </summary>
        /// <returns><c>true</c>, 発信可能, <c>false</c> 発信不可</returns>
        public bool TransmissionSupported()
        {
            int checkResult = BeaconTransmitter.CheckTransmissionSupported(Android.App.Application.Context);

            return(checkResult == BeaconTransmitter.Supported);
        }