public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.layout_sync_bluetooth, container, false);

            string possible = BluetoothHelper.isPossible();

            if (possible != "")
            {
                view.FindViewById <TextView>(Resource.Id.sb_text).Text = possible;
                return(view);
            }

            view.FindViewById <TextView>(Resource.Id.sb_text).Text = "Dein GeräteName: " + BluetoothHelper.getDeviceName();

            ListView list = view.FindViewById <ListView>(Resource.Id.sb_list);

            list.Adapter           = new ArrayAdapter <string>(Context, Resource.Layout.sb_listitem, BluetoothHelper.getAvaiableDeviceNames());
            list.TextFilterEnabled = true;
            list.ItemClick        += (sender, e) =>
            {
                BluetoothDevice device = BluetoothHelper.GetDeviceByName(((TextView)e.View).Text);


                if (device != null)
                {
                    BluetoothHelper.GetAdapter().CancelDiscovery();
                    Console.WriteLine(device.GetType().ToString());
                    device.CreateBond();
                    Type       t    = device.GetType();
                    MethodInfo info = t.GetMethod("CreateRfcommSocket");
                    object     o    = info.Invoke(device, new object[] { 1 });
                    //		Name	"CreateRfcommSocketToServiceRecord"	string

                    var socket = (BluetoothSocket)o;
                    //var socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                    socket.Connect();

                    BTDeviceArgs args = new BTDeviceArgs();
                    args.socket = socket;
                    finished(this, args);
                }
            };

            return(view);
        }
示例#2
0
        public async Task Pair()
        {
            var methods = BluetoothDevice.GetType().GetMethods().ToList();

            foreach (var methodInfo in methods)
            {
            }

            var method = BluetoothDevice.GetType().GetMethod("CreateBond");

            method.Invoke(BluetoothDevice, null);
        }
        private void ProbeConnection()
        {
            ParcelUuid[] parcelUuids;
            parcelUuids = Device.GetUuids();
            bool isConnected = false;



            if (parcelUuids == null)
            {
                parcelUuids    = new ParcelUuid[1];
                parcelUuids[0] = new ParcelUuid(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
            }

            foreach (var parcelUuid in parcelUuids)
            {
                mBluetoothAdapter.CancelDiscovery();
                //METHOD A

                try
                {
                    var method = Device.GetType().GetMethod("createRfcommSocket");
                    mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
                    mmSocket.Connect();
                    isConnected = true;
                    DoDeviceConnected();
                    break;
                }
                catch (Exception)
                {
                }

                //METHOD B

                try
                {
                    var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
                    mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
                    mmSocket.Connect();
                    isConnected = true;
                    DoDeviceConnected();
                    break;
                }
                catch (Exception)
                {
                }
            }

            if (!isConnected)
            {
                //METHOD C

                try
                {
                    IntPtr createRfcommSocket = JNIEnv.GetMethodID(Device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
                    IntPtr _socket            = JNIEnv.CallObjectMethod(Device.Handle, createRfcommSocket, new global::Android.Runtime.JValue(Port));
                    mmSocket = Java.Lang.Object.GetObject <BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);

                    mmSocket.Connect();
                    DoDeviceConnected();
                }
                catch (IOException connectException)
                {
                    // Unable to connect; close the socket and get out
                    try
                    {
                        mmSocket.Close();
                    }
                    catch (IOException closeException)
                    {
                    }
                    DoDeviceConnectionFailed();
                }
            }
        }
示例#4
0
        private void ProbeConnection()
        {
            ParcelUuid[] parcelUuids;
            parcelUuids = m_device.GetUuids();
            bool isConnected = false;

            if (parcelUuids == null)
            {
                parcelUuids    = new ParcelUuid[1];
                parcelUuids[0] = new ParcelUuid(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
            }

            m_bluetoothAdapter.CancelDiscovery();


            //METHOD A

            try
            {
                var method = m_device.GetType().GetMethod("createRfcommSocket");
                Socket = (BluetoothSocket)method.Invoke(m_device, new object[] { m_port });
                Socket.Connect();
                isConnected = true;
                DoDeviceConnected();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Faield to createRfcommSocket {ex}");
            }

            if (!isConnected)
            {
                //METHOD B

                try
                {
                    var method = m_device.GetType().GetMethod("createInsecureRfcommSocket");
                    Socket = (BluetoothSocket)method.Invoke(m_device, new object[] { m_port });
                    Socket.Connect();
                    isConnected = true;
                    DoDeviceConnected();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"Faield to createInsecureRfcommSocket {ex}");
                }
            }

            if (!isConnected)
            {
                //METHOD C

                try
                {
                    IntPtr createRfcommSocket = JNIEnv.GetMethodID(m_device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
                    IntPtr _socket            = JNIEnv.CallObjectMethod(m_device.Handle, createRfcommSocket, new global::Android.Runtime.JValue(m_port));
                    Socket = Java.Lang.Object.GetObject <BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);
                    Socket.Connect();
                    isConnected = true;
                    DoDeviceConnected();
                }
                catch (IOException ex)
                {
                    System.Diagnostics.Debug.WriteLine($"Faield to createRfcommSocket JNI, {ex}");
                }
            }

            if (!isConnected)
            {
                // Unable to connect; close the socket and get out
                if (Socket != null)
                {
                    try
                    {
                        Socket.Close();
                    }
                    catch (IOException e)
                    {
                        System.Diagnostics.Debug.WriteLine($"Faield to close socket, {e}");
                    }
                }
                DoDeviceConnectionFailed();
            }
        }
        private void ProbeConnection()
        {
            ParcelUuid[] parcelUuids;
            parcelUuids = Device.GetUuids();
            bool isConnected = false;

            if (parcelUuids == null)
            {
                throw new System.Exception("No Unique Identifiers found");
            }
            else
            {
                Debug.WriteLine("Connecting...");
                foreach (var parcelUuid in parcelUuids)
                {
                    mBluetoothAdapter.CancelDiscovery();
                    //METHOD A

                    try
                    {
                        var method = Device.GetType().GetMethod("createRfcommSocket");
                        mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
                        mmSocket.Connect();
                        Debug.WriteLine("Connected...");
                        isConnected = true;
                        DoDeviceConnected();
                        break;
                    }
                    catch (Exception)
                    {
                    }

                    //METHOD B

                    try
                    {
                        var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
                        mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
                        mmSocket.Connect();
                        Debug.WriteLine("Connected...");
                        isConnected = true;
                        DoDeviceConnected();
                        break;
                    }
                    catch (Exception)
                    {
                    }
                }

                if (!isConnected)
                {
                    //METHOD C

                    try
                    {
                        IntPtr createRfcommSocket = JNIEnv.GetMethodID(Device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
                        // JNIEnv.GetMethodID(device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
                        IntPtr _socket = JNIEnv.CallObjectMethod(Device.Handle, createRfcommSocket, new global::Android.Runtime.JValue(Port));
                        mmSocket = Java.Lang.Object.GetObject <BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);

                        /*
                         * mmSocket =
                         *  mBluetoothAdapter.GetRemoteDevice(Device.Address)
                         *      .CreateRfcommSocketToServiceRecord(
                         *          UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
                         */
                        mmSocket.Connect();
                        DoDeviceConnected();
                    }
                    catch (IOException connectException)
                    {
                        Debug.WriteLine("IO Error: " + connectException.Message);
                        // Unable to connect; close the socket and get out
                        try
                        {
                            mmSocket.Close();
                        }
                        catch (IOException closeException)
                        {
                            Debug.WriteLine("Error: " + closeException.Message);
                        }
                        throw new BluetoothDeviceNotFoundException(connectException.Message);
                        return;
                    }
                }
            }
        }