void Start()
    {
        Debug.Log("Bluetooth Initializing...");
        BluetoothDeviceScript receiver = BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {
            Debug.Log("Bluetooth Initialized.");
            Debug.Log("Start scanning...");
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, (deviceID, name, rssi, adInfo) =>
            {
                //  deviceID:   安卓上是 MAC 地址,iOS 上是某一串不明代码
                //  adInfo:     广播数据,第 2-7 字节是 MAC 地址
                if (("R-ARCHER" == name) || ("GU-ARCHER" == name))
                {
                    Debug.Log("Found: " + name);
                    //BluetoothLEHardwareInterface.StopScan();

                    GeekplayDevice device = GetDeviceScript(name);
                    device.SetAppInfo("", "", "", "");
                    device.m_deviceID = deviceID;
                    device.m_MAC      = deviceID.Replace(":", "");

                    deviceList.Add(device);
                    if (typeof(GeekplayHunter) == device.GetType())
                    {
                        ((GeekplayHunter)device).Initialize(BowDraw, BowShoot);
                    }
                    else if (typeof(GeekplayDragonbone) == device.GetType())
                    {
                        ((GeekplayDragonbone)device).Initialize(BowDraw, BowShoot);
                    }
                }
            });
        }, null);
    }
Example #2
0
 private void Awake()
 {
     if (this.debugOutput)
     {
         Debug.Log("BluetoothLE.Awake called");
     }
     if (BluetoothLE.created)
     {
         UnityEngine.Object.Destroy(base.gameObject);
     }
     else
     {
         UnityEngine.Object.DontDestroyOnLoad(this);
         BluetoothLE.bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, new Action(this.Initialized), new Action <string>(this.Error));
         if (null != BluetoothLE.bluetoothDeviceScript)
         {
             UnityEngine.Object.DontDestroyOnLoad(BluetoothLE.bluetoothDeviceScript);
             BluetoothLE.bluetoothDeviceScript.DisconnectedPeripheralAction = new Action <string>(this.disconnectedPeripheralAction);
         }
         BluetoothLE.created = true;
         if (this.debugOutput)
         {
             Debug.Log("BluetoothLE.Awake initialized");
         }
         BluetoothLE.instance = this;
     }
     Array.Clear(this.status, 0, (int)this.status.Length);
     Array.Clear(this.config, 0, (int)this.config.Length);
     Array.Clear(this.mapping, 0, (int)this.mapping.Length);
 }
Example #3
0
    public static BluetoothDeviceScript Initialize(bool asCentral, bool asPeripheral, Action action, Action <string> errorAction)
    {
        bluetoothDeviceScript = null;

#if UNITY_2018_3_OR_NEWER
#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.CoarseLocation))
        {
            Permission.RequestUserPermission(Permission.CoarseLocation);
        }
#endif
#endif

        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");
        if (bluetoothLEReceiver == null)
        {
            bluetoothLEReceiver = new GameObject("BluetoothLEReceiver");
        }

        if (bluetoothLEReceiver != null)
        {
            bluetoothDeviceScript = bluetoothLEReceiver.AddComponent <BluetoothDeviceScript> ();
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.InitializedAction = action;
                bluetoothDeviceScript.ErrorAction       = errorAction;
            }
        }

        GameObject.DontDestroyOnLoad(bluetoothLEReceiver);

        if (Application.isEditor)
        {
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.SendMessage("OnBluetoothMessage", "Initialized");
            }
        }
        else
        {
#if UNITY_IPHONE || UNITY_TVOS
            _iOSBluetoothLEInitialize(asCentral, asPeripheral);
#elif UNITY_ANDROID
            if (_android == null)
            {
                AndroidJavaClass javaClass = new AndroidJavaClass("com.shatalmic.unityandroidbluetoothlelib.UnityBluetoothLE");
                _android = javaClass.CallStatic <AndroidJavaObject> ("getInstance");
            }

            if (_android != null)
            {
                _android.Call("androidBluetoothInitialize", asCentral, asPeripheral);
            }
#endif
        }

        return(bluetoothDeviceScript);
    }
    public void Init(){

		Instance = this;

		_bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize (
			true, false, 
			Scan, 
		(e) => {ScreenManager.Instance.ShowError("Error: " + e);});
        
    }
Example #5
0
    // Use this for initialization
    void Start()
    {
        _screenHeight = Camera.main.orthographicSize * 2f;
        _screenWidth  = _screenHeight * Camera.main.aspect;

        //initialize itself as a central device
        bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, null, null);

        //request the screen to not sleep
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }
Example #6
0
    public static void FinishDeInitialize()
    {
        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");

        if (bluetoothLEReceiver != null)
        {
            GameObject.Destroy(bluetoothLEReceiver);
        }
        bluetoothDeviceScript = null;
        bluetoothLEReceiver   = null;
    }
Example #7
0
 // Use this for initialization
 public void Init(IErgDataCallbacks ergCallbacks)
 {
     this.ergCallbacks = ergCallbacks;
     // output.text = "Initializing bluetooth";
     // byte[] data = new byte[] {69,9};
     // output1.text = String.Format("{0}{1}",data[0],data[1]);
     bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, () => {
         // output.text = "Diente azul inicializado";
         ScanForPM5Ergs();
     }, null);
     // initActions ();
 }
Example #8
0
 void Awake()
 {
     if (Instance == null)
     {
         DontDestroyOnLoad(gameObject);
         Instance = this;
     }
     else if (Instance != this)
     {
         Destroy(gameObject);
     }
 }
Example #9
0
 void StartProcess()
 {
     reset();
     // Init as Central BLE device
     BlueScript = BluetoothLEHardwareInterface.Initialize(true, false, () => {
         SetState(machineState.Scan, 0.1f);
         state = State.Scanning;
     }, (error) => {
         state = State.Error;
         BluetoothLEHardwareInterface.Log("Error during initialize: " + error);
     });
     BlueScript.DisconnectedPeripheralAction += Disconnected;
 }
    public static BluetoothDeviceScript Initialize(bool asCentral, bool asPeripheral, Action action, Action <string> errorAction)
    {
        bluetoothDeviceScript = null;

        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");

        if (bluetoothLEReceiver == null)
        {
            bluetoothLEReceiver = new GameObject("BluetoothLEReceiver");

            bluetoothDeviceScript = bluetoothLEReceiver.AddComponent <BluetoothDeviceScript>();
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.InitializedAction = action;
                bluetoothDeviceScript.ErrorAction       = errorAction;
            }
        }

        GameObject.DontDestroyOnLoad(bluetoothLEReceiver);

        if (Application.isEditor)
        {
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.SendMessage("OnBluetoothMessage", "Initialized");
            }
        }
        else
        {
#if UNITY_IPHONE || UNITY_TVOS
            _iOSBluetoothLEInitialize(asCentral, asPeripheral);
#elif UNITY_ANDROID
            if (_android == null)
            {
                AndroidJavaClass javaClass = new AndroidJavaClass("com.shatalmic.unityandroidbluetoothlelib.UnityBluetoothLE");
                _android = javaClass.CallStatic <AndroidJavaObject> ("getInstance");
                Debug.Log("CFK-android class");
            }

            if (_android != null)
            {
                _android.Call("androidBluetoothInitialize", asCentral, asPeripheral);
            }
            Debug.Log("CFK-android class baslattik");
#endif
        }

        return(bluetoothDeviceScript);
    }
Example #11
0
    void InitBluetooth(DeviceName[] _supportedDevices)
    {
        Debug.Log("Bluetooth Initializing...");
        BluetoothDeviceScript receiver = BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {
            Debug.Log("Bluetooth Initialized.");
            Scan(_supportedDevices);
        }, (err) =>
        {
            Debug.Log("Bluetooth Error: " + err);
            if ("Bluetooth LE Not Enabled" == err)
            {
                BluetoothLEHardwareInterface.FinishDeInitialize();
                BluetoothLEHardwareInterface.DeInitialize(() => { StartCoroutine(ReInitBluetooth(_supportedDevices)); });
            }
        });

        DontDestroyOnLoad(receiver.gameObject);
    }
Example #12
0
    public static BluetoothDeviceScript Initialize(bool asCentral, bool asPeripheral, Action action, Action <string> errorAction)
    {
        bluetoothDeviceScript = null;

        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");

        if (bluetoothLEReceiver == null)
        {
            bluetoothLEReceiver = new GameObject("BluetoothLEReceiver");

            bluetoothDeviceScript = bluetoothLEReceiver.AddComponent <BluetoothDeviceScript>();
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.InitializedAction = action;
                bluetoothDeviceScript.ErrorAction       = errorAction;
            }
        }

        GameObject.DontDestroyOnLoad(bluetoothLEReceiver);

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        // Register for updates
        _winBluetoothLEConnectCallbacks(UnitySendMessageWrapper, DebugLog, DebugLogWarning, DebugLogError);
        _winBluetoothLEInitialize(asCentral, asPeripheral);
#elif (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR_OSX
        _iOSBluetoothLEInitialize(asCentral, asPeripheral);
#elif UNITY_ANDROID
        if (_android == null)
        {
            AndroidJavaClass javaClass = new AndroidJavaClass("com.shatalmic.unityandroidbluetoothlelib.UnityBluetoothLE");
            _android = javaClass.CallStatic <AndroidJavaObject> ("getInstance");
        }

        if (_android != null)
        {
            _android.Call("androidBluetoothInitialize", asCentral, asPeripheral);
        }
#endif

        return(bluetoothDeviceScript);
    }
Example #13
0
 public void FinishAndResetAPI(Action callback = null)
 {
     CancelInvoke ();
     hostMode = false;
     Advertising = false;
     isConnected = false;
     pairedUserData = null;
     fightSetting = null;
     RTTTime = 0;
     CalculatingRTT = false;
     ResponseTimer = 0;
     myResult = null;
     targetResult = null;
     fullUserFightResult = null;
     BluetoothLEHardwareInterface.DeInitialize (callback);
     bluetoothDeviceScript = null;
 }
Example #14
0
 public BluetoothDeviceScriptWrapper(BluetoothDeviceScript bluetoothDeviceScript)
 {
     this.bluetoothDeviceScript = bluetoothDeviceScript;
 }
Example #15
0
    public static BluetoothDeviceScript Initialize(bool asCentral, bool asPeripheral, Action action, Action <string> errorAction)
    {
        bluetoothDeviceScript = null;

#if UNITY_2018_3_OR_NEWER
#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
        {
            Permission.RequestUserPermission(Permission.FineLocation);
        }
#endif
#endif

        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");
        if (bluetoothLEReceiver == null)
        {
            bluetoothLEReceiver = new GameObject("BluetoothLEReceiver");
        }

        if (bluetoothLEReceiver != null)
        {
            bluetoothDeviceScript = bluetoothLEReceiver.GetComponent <BluetoothDeviceScript> ();
            if (bluetoothDeviceScript == null)
            {
                bluetoothDeviceScript = bluetoothLEReceiver.AddComponent <BluetoothDeviceScript> ();
            }

            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.InitializedAction = action;
                bluetoothDeviceScript.ErrorAction       = errorAction;
            }
        }

        GameObject.DontDestroyOnLoad(bluetoothLEReceiver);

#if EXPERIMENTAL_MACOS_EDITOR && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX)
        ConnectUnitySendMessageCallback((objectName, commandName, commandData) => {
            string name    = Marshal.PtrToStringAuto(objectName);
            string command = Marshal.PtrToStringAuto(commandName);
            string data    = Marshal.PtrToStringAuto(commandData);

            GameObject foundObject = GameObject.Find(name);
            if (foundObject != null)
            {
                foundObject.SendMessage(command, data);
            }
        });

        BluetoothLEHardwareInterface.OSXBluetoothLEInitialize(asCentral, asPeripheral);
#else
        if (Application.isEditor)
        {
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.SendMessage("OnBluetoothMessage", "Initialized");
            }
        }
        else
        {
#if UNITY_IOS || UNITY_TVOS
            _iOSBluetoothLEInitialize(asCentral, asPeripheral);
#elif UNITY_ANDROID
            if (_android == null)
            {
                AndroidJavaClass javaClass = new AndroidJavaClass("com.shatalmic.unityandroidbluetoothlelib.UnityBluetoothLE");
                _android = javaClass.CallStatic <AndroidJavaObject> ("getInstance");
            }

            if (_android != null)
            {
                _android.Call("androidBluetoothInitialize", asCentral, asPeripheral);
            }
#endif
        }
#endif

        return(bluetoothDeviceScript);
    }
Example #16
0
    void OnGUI()
    {
        GUI.skin = skin;
        GUIStyle myButtonStyle = new GUIStyle(GUI.skin.button);

        myButtonStyle.fontSize = 20;

        if (bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && devices.Count > 0)
        {
            int i = 0;
            foreach (KeyValuePair <string, string> kv in devices)
            {
                if (GUI.Button(new Rect(310, 100 * (i + 1), 300, 100), string.Format("{0} {1}", kv.Key, kv.Value), myButtonStyle))
                {
                    if (deviceUUID != "")
                    {
                        BluetoothLEHardwareInterface.DisconnectPeripheral(deviceUUID, null);
                    }

                    deviceUUID = kv.Key;
                    serviceUUIDs.Clear();
                    BluetoothLEHardwareInterface.StopScan();
                    BluetoothLEHardwareInterface.ConnectToPeripheral(kv.Key, null, null
                                                                     , (address, serviceUUID, characteristicUUID) =>
                    {
                        if (!serviceUUIDs.ContainsKey(serviceUUID))
                        {
                            List <string> charList = new List <string> {
                                characteristicUUID
                            };
                            serviceUUIDs.Add(serviceUUID, charList);
                        }
                        else
                        {
                            serviceUUIDs[serviceUUID].Add(characteristicUUID);
                        }
                    }
                                                                     );
                }
                i++;
            }
        }

        int j = 0;

        foreach (KeyValuePair <string, List <string> > kv in serviceUUIDs)
        {
            foreach (string c in kv.Value)
            {
                if (GUI.Button(new Rect(610, 100 * (j + 1), 1200, 100), string.Format("srv:{0} char:{1}", kv.Key, c), myButtonStyle))
                {
                    selectServiceUUID        = kv.Key;
                    selectCharacteristicUUID = c;
                }
                j++;
            }
        }

        if (GUI.Button(new Rect(10, 0, 300, 50), "DeInitialize", myButtonStyle))
        {
            BluetoothLEHardwareInterface.DeInitialize(null);
        }

        if (GUI.Button(new Rect(10, 50, 300, 100), "Initialize Central", myButtonStyle))
        {
            bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, null, null);
        }

        if (GUI.Button(new Rect(10, 150, 300, 100), "Scan for Any", myButtonStyle))
        {
            devices.Clear();
            serviceUUIDs.Clear();
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, name) =>
            {
                devices[address] = name;
            });
        }

//		if (GUI.Button (new Rect (10, 250, 300, 50), "Stop Scan"))
//			BluetoothLEHardwareInterface.StopScan ();

        if (GUI.Button(new Rect(10, 250, 300, 100), BitConverter.ToString(new byte[] { 1, 2, 10 }) + "Read Characteristic", myButtonStyle) && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(deviceUUID, selectServiceUUID, selectCharacteristicUUID, null, (address, characteristicUUID, data) =>
            {
                recvName = address;
                recvData = BitConverter.ToString(data);
            }
                                                                                  );
        }
        GUI.TextArea(new Rect(10, 350, 300, 100), recvName + "\n" + recvData);


        sendData = GUI.TextField(new Rect(10, 550, 300, 100), sendData, 64);
        if (GUI.Button(new Rect(10, 450, 300, 100), "Write Characteristic", myButtonStyle) && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            if (sendData == null)
            {
                data = new byte[64];
                for (int i = 0; i < 64; ++i)
                {
                    data [i] = (byte)i;
                }
            }
            else
            {
                data = System.Text.Encoding.Default.GetBytes(sendData);
            }

            BluetoothLEHardwareInterface.WriteCharacteristic(deviceUUID, selectServiceUUID, selectCharacteristicUUID, data, data.Length, true, null);
        }
    }
Example #17
0
    void OnGUI()
    {
        GUI.skin = skin;

        if (GUI.Button(new Rect(10, 0, 600, 100), "DeInitialize"))
        {
            BluetoothLEHardwareInterface.DeInitialize(null);
        }

        if (GUI.Button(new Rect(10, 100, 300, 50), "Initialize Central"))
        {
            bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, null, null);
        }

        if (GUI.Button(new Rect(10, 150, 300, 50), "Scan for 1851"))
        {
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(new string[] { serviceUUID }, null);
        }

        if (GUI.Button(new Rect(10, 200, 300, 50), "Scan for Any"))
        {
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null);
        }

        if (GUI.Button(new Rect(10, 250, 300, 50), "Retrieve Connected"))
        {
            BluetoothLEHardwareInterface.RetrieveListOfPeripheralsWithServices(new string[] { serviceUUID }, null);
        }

        if (GUI.Button(new Rect(10, 300, 300, 50), "Stop Scan"))
        {
            BluetoothLEHardwareInterface.StopScan();
        }

        if (GUI.Button(new Rect(10, 350, 300, 50), "Connect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.ConnectToPeripheral(bluetoothDeviceScript.DiscoveredDeviceList[0], null, null, null);
        }

        if (GUI.Button(new Rect(10, 400, 300, 50), "Disconnect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.DisconnectPeripheral(bluetoothDeviceScript.DiscoveredDeviceList[0], null);
        }

        if (GUI.Button(new Rect(10, 450, 300, 50), "Read Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.ReadCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
        }

        if (GUI.Button(new Rect(10, 500, 300, 50), "Write Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            if (data == null)
            {
                data = new byte[64];
                for (int i = 0; i < 64; ++i)
                {
                    data[i] = (byte)i;
                }
            }

            BluetoothLEHardwareInterface.WriteCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, data, data.Length, true, null);
        }

        if (GUI.Button(new Rect(10, 550, 300, 50), "Subscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.SubscribeCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null, null);
        }

        if (GUI.Button(new Rect(10, 600, 300, 50), "UnSubscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.UnSubscribeCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
        }

        if (GUI.Button(new Rect(310, 100, 300, 100), "Initialize Peripheral"))
        {
            BluetoothLEHardwareInterface.Initialize(false, true, null, null);
        }

        if (GUI.Button(new Rect(310, 200, 300, 100), "Create Service\nand Characteristic"))
        {
            BluetoothLEHardwareInterface.PeripheralName("Test Device");

            if (data == null)
            {
                data = new byte[64];
                for (int i = 0; i < 64; ++i)
                {
                    data[i] = (byte)i;
                }
            }

            BluetoothLEHardwareInterface.CreateCharacteristic(characteristicUUID,
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable |
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable,
                                                              null, 0, null);

            BluetoothLEHardwareInterface.CreateService(serviceUUID, true, null);
        }

        if (GUI.Button(new Rect(310, 300, 300, 100), "Start Advertising"))
        {
            BluetoothLEHardwareInterface.StartAdvertising(null);
        }

        if (GUI.Button(new Rect(310, 400, 300, 100), "Stop Advertising"))
        {
            BluetoothLEHardwareInterface.StopAdvertising(null);
        }

        if (GUI.Button(new Rect(310, 500, 300, 100), "Update Characteristic Value"))
        {
            for (int i = 0; i < data.Length; ++i)
            {
                data[i] = (byte)(data[i] + 1);
            }

            BluetoothLEHardwareInterface.UpdateCharacteristicValue(characteristicUUID, data, data.Length);
        }
    }
    public static BluetoothDeviceScript Initialize(bool asCentral, bool asPeripheral, Action action, Action<string> errorAction)
    {
        bluetoothDeviceScript = null;

        GameObject bluetoothLEReceiver = GameObject.Find("BluetoothLEReceiver");
        if (bluetoothLEReceiver == null)
        {
            bluetoothLEReceiver = new GameObject("BluetoothLEReceiver");

            bluetoothDeviceScript = bluetoothLEReceiver.AddComponent<BluetoothDeviceScript>();
            if (bluetoothDeviceScript != null)
            {
                bluetoothDeviceScript.InitializedAction = action;
                bluetoothDeviceScript.ErrorAction = errorAction;
            }
        }

        GameObject.DontDestroyOnLoad (bluetoothLEReceiver);

        if (Application.isEditor)
        {
            if (bluetoothDeviceScript != null)
                bluetoothDeviceScript.SendMessage ("OnBluetoothMessage", "Initialized");
        }
        else
        {
        #if UNITY_IPHONE
            _iOSBluetoothLEInitialize (asCentral, asPeripheral);
        #elif UNITY_ANDROID
            if (_android == null)
            {
                AndroidJavaClass javaClass = new AndroidJavaClass ("com.shatalmic.unityandroidbluetoothlelib.UnityBluetoothLE");
                _android = javaClass.CallStatic<AndroidJavaObject> ("getInstance");
            }

            if (_android != null)
                _android.Call ("androidBluetoothInitialize", asCentral, asPeripheral);
        #endif
        }

        return bluetoothDeviceScript;
    }
Example #19
0
    public void TryToPair()
    {
        Debug.Log("Try To Pair");
        hostMode = false;

        bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize (true, false, ()=>
        {
            Invoke("ScanForPeripheralsWithServices",0);
        }, OnError);
    }
Example #20
0
	void OnGUI ()
	{
		GUI.skin = skin;

		if (GUI.Button (new Rect (10, 0, 600, 100), "DeInitialize"))
			BluetoothLEHardwareInterface.DeInitialize (null);
		
		if (GUI.Button (new Rect (10, 100, 300, 50), "Initialize Central"))
			bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize (true, false, null, null);

		if (GUI.Button (new Rect (10, 150, 300, 50), "Scan for 1851"))
			BluetoothLEHardwareInterface.ScanForPeripheralsWithServices (new string[] { serviceUUID }, null);
		
		if (GUI.Button (new Rect (10, 200, 300, 50), "Scan for Any"))
			BluetoothLEHardwareInterface.ScanForPeripheralsWithServices (null, null);
		
		if (GUI.Button (new Rect (10, 250, 300, 50), "Retrieve Connected"))
			BluetoothLEHardwareInterface.RetrieveListOfPeripheralsWithServices (new string[] { serviceUUID }, null);

		if (GUI.Button (new Rect (10, 300, 300, 50), "Stop Scan"))
			BluetoothLEHardwareInterface.StopScan ();
		
		if (GUI.Button (new Rect (10, 350, 300, 50), "Connect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
			BluetoothLEHardwareInterface.ConnectToPeripheral (bluetoothDeviceScript.DiscoveredDeviceList[0], null, null, null);
		
		if (GUI.Button (new Rect (10, 400, 300, 50), "Disconnect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
			BluetoothLEHardwareInterface.DisconnectPeripheral (bluetoothDeviceScript.DiscoveredDeviceList[0], null);

		if (GUI.Button (new Rect (10, 450, 300, 50), "Read Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
			BluetoothLEHardwareInterface.ReadCharacteristic (bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
		
		if (GUI.Button (new Rect (10, 500, 300, 50), "Write Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
		{
			if (data == null)
			{
				data = new byte[64];
				for (int i = 0; i < 64; ++i)
					data[i] = (byte)i;
			}

			BluetoothLEHardwareInterface.WriteCharacteristic (bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, data, data.Length, true, null);
		}

		if (GUI.Button (new Rect (10, 550, 300, 50), "Subscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
			BluetoothLEHardwareInterface.SubscribeCharacteristic (bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null, null);

		if (GUI.Button (new Rect (10, 600, 300, 50), "UnSubscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
			BluetoothLEHardwareInterface.UnSubscribeCharacteristic (bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
		
		if (GUI.Button (new Rect (310, 100, 300, 100), "Initialize Peripheral"))
			BluetoothLEHardwareInterface.Initialize (false, true, null, null);

		if (GUI.Button (new Rect (310, 200, 300, 100), "Create Service\nand Characteristic"))
		{
			BluetoothLEHardwareInterface.PeripheralName ("Test Device");

			if (data == null)
			{
				data = new byte[64];
				for (int i = 0; i < 64; ++i)
					data[i] = (byte)i;
			}

			BluetoothLEHardwareInterface.CreateCharacteristic (characteristicUUID, 
			                                                   BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead | 
			                                                   BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite | 
			                                                   BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify, 
			                                                   BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable |
			                                                   BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable, 
			                                                   null, 0, null);

			BluetoothLEHardwareInterface.CreateService (serviceUUID, true, null);
		}
		
		if (GUI.Button (new Rect (310, 300, 300, 100), "Start Advertising"))
			BluetoothLEHardwareInterface.StartAdvertising (null);
		
		if (GUI.Button (new Rect (310, 400, 300, 100), "Stop Advertising"))
			BluetoothLEHardwareInterface.StopAdvertising (null);
		
		if (GUI.Button (new Rect (310, 500, 300, 100), "Update Characteristic Value"))
		{
			for (int i = 0; i < data.Length; ++i)
				data[i] = (byte)(data[i] + 1);

			BluetoothLEHardwareInterface.UpdateCharacteristicValue (characteristicUUID, data, data.Length);
		}
	}