示例#1
0
        public BluetoothStream(BluetoothDevice btdev)
        {
            socket = new Socket((AddressFamily)32, SocketType.Stream, (ProtocolType)3);
            byte[] btAddress = btdev.Address;
            try
            {
                BluetoothEndPoint btep = new BluetoothEndPoint(btAddress, guid_spp);
                utils.ddump("connecting to " + btdev.AddressStr);
                socket.Connect(btep);
                utils.ddump("connected! Create networkstream...");
                networkstream = new NetworkStream(socket, true);

                utils.ddump("starting threads...");
                thSend = new Thread(new ThreadStart(sendThread));
                thSend.Name = "SendThread"; thSend.IsBackground = true;
                thSend.Start();
                utils.ddump("Thread started: " + thSend.Name + ", " + thSend.ManagedThreadId.ToString("x08"));

                thRecv = new Thread(new ThreadStart(recvThread));
                thRecv.Name = "RecvThread"; thRecv.IsBackground = true;
                thRecv.Start();
                utils.ddump("Thread started: " + thRecv.Name + ", " + thRecv.ManagedThreadId.ToString("x08"));
            }
            catch (Exception ex)
            {
                connected = false;
                utils.ddump("BluetoothStream init failed with " + ex.Message);
            }
        }
示例#2
0
        //:this(btAddress)
        public BluetoothStream(byte[] btAddress, string sPin)
        {
            _btAddress = btAddress;
            if (sPin != String.Empty && sPin != "")
                setPIN(btAddress, sPin);
            socket = new Socket((AddressFamily)32, SocketType.Stream, (ProtocolType)3);
            try
            {
                BluetoothEndPoint btep = new BluetoothEndPoint(btAddress, guid_spp);
                socket.Connect(btep);

                networkstream = new NetworkStream(socket, true);

                thSend = new Thread(new ThreadStart(sendThread));
                thSend.Name = "SendThread";
                thSend.Start();
                utils.ddump("Thread started: " + thSend.Name + ", " + thSend.ManagedThreadId.ToString());

                thRecv = new Thread(new ThreadStart(recvThread));
                thRecv.Name = "RecvThread";
                thRecv.Start();
                utils.ddump("Thread started: " + thRecv.Name + ", " + thRecv.ManagedThreadId.ToString());
            }
            catch (Exception ex)
            {
                connected = false;
                utils.ddump("BluetoothStream init failed with "+ex.Message);
            }
        }
示例#3
0
        public NetworkStream Connect(byte[] btAddress, Guid serviceGuid, bool secure)
        {
            Socket clientSocket = new Socket((AddressFamily)32, SocketType.Stream, (ProtocolType)3);
            NetworkStream nws = null;

            this.address = btAddress;
            try
            {
                BluetoothEndPoint endPoint = new BluetoothEndPoint(btAddress, serviceGuid);

                clientSocket.Connect(endPoint);
                if (secure)
                {
                    // turn on authentication
                    SafeNativeMethods.BthAuthenticate(this.address);
                    // turn on link encryption
                    SafeNativeMethods.BthSetEncryption(this.address, 1);
                }
                nws = new NetworkStream(clientSocket, true);
                connected = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in Connect(): " + ex.Message);
                nws = null;
                connected = false; ;
            }

            return nws;
        }
示例#4
0
        private void PublishService()
        {
            serverSocket = new Socket((AddressFamily)32, SocketType.Stream, (ProtocolType)3);

            BluetoothEndPoint btep = new BluetoothEndPoint();

            serverSocket.Bind(btep);

            // extract port number
            int port = ((BluetoothEndPoint)serverSocket.LocalEndPoint).Port;

            byte[] sdpRecord = GenerateSDPRecord(ServiceGuid, port);

            // set up BTHNS_BLOB

            byte[] bthnsBlob = new byte[36 + sdpRecord.Length];

            int sdpVersion = 1;
            int serviceHandle = 0;

            GCHandle sdpVersionHandle = GCHandle.Alloc(sdpVersion, GCHandleType.Pinned);
            GCHandle recordHandle = GCHandle.Alloc(serviceHandle, GCHandleType.Pinned);

            BitConverter.GetBytes(sdpVersionHandle.AddrOfPinnedObject().ToInt32()).CopyTo(bthnsBlob, 0);
            BitConverter.GetBytes(recordHandle.AddrOfPinnedObject().ToInt32()).CopyTo(bthnsBlob, 4);

            BitConverter.GetBytes(sdpRecord.Length).CopyTo(bthnsBlob, 32);

            Buffer.BlockCopy(sdpRecord, 0, bthnsBlob, 36, sdpRecord.Length);

            // end BTHNS_BLOB

            // create a BLOB to hold the BTHNS_BLOB

            byte[] querySetBlob = new byte[8];

            GCHandle bthnsBlobHandle = GCHandle.Alloc(bthnsBlob, GCHandleType.Pinned);

            BitConverter.GetBytes(80).CopyTo(querySetBlob, 0);
            BitConverter.GetBytes(bthnsBlobHandle.AddrOfPinnedObject().ToInt32()).CopyTo(querySetBlob, 4);

            // end BLOB

            // query set setup
            const int querySetLengthOffset = 0;
            const int querySetNamespaceOffset = 20;
            const int querySetLength = 60;
            const int NS_BTH = 16;

            byte[] querySet = new byte[querySetLength];

            BitConverter.GetBytes(querySetLength).CopyTo(querySet, querySetLengthOffset);
            BitConverter.GetBytes(NS_BTH).CopyTo(querySet, querySetNamespaceOffset);

            GCHandle querySetBlobHandle = GCHandle.Alloc(querySetBlob, GCHandleType.Pinned);

            BitConverter.GetBytes(querySetBlobHandle.AddrOfPinnedObject().ToInt32()).CopyTo(querySet, 56);

            // end query set setup

            // register the service

            int result = SafeNativeMethods.WSASetService(querySet, 0, 0);

            querySetBlobHandle.Free();
            bthnsBlobHandle.Free();
            sdpVersionHandle.Free();
            recordHandle.Free();

            // end register service
        }