PDA

View Full Version : سوال: دريافت ديتا از طريق بلوتوث



a1malek
پنج شنبه 19 آذر 1394, 14:34 عصر
سلام دوستان من ميخوام يك مقدار رشته اي كه از طريق بلوتوث اندرويد ارسال كردم رو توي نرم افزار خودم دريافت كنم و بهش دست رسي داشته باشم
هر گونه كمكي اعم از نمونه كد به هر زبوني يا كمك تئوري و هم فكري و حتي موفق باشيد گفتن مفيد واقع خواهد شد فقط جان من لينك هاي MSDN رو بهم پاس ندين :لبخند: :بوس: يك دنيا تشكر

abdullah20
پنج شنبه 19 آذر 1394, 16:10 عصر
بیشتر توضیح بدید
رشته که از موبایل ارسال میشه به چه طریق ارسال میشه؟

a1malek
پنج شنبه 19 آذر 1394, 19:36 عصر
این کد ارسال رشته در اندروید:



BluetoothDevice device;


BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();


final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
String mac = "AC:72:89:CF:F9:E9"; //آدرس مک لبتاب که رشته رو بهش میفرستم


device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
// Get a BluetoothSocket to connect with the given BluetoothDevice
BluetoothSocket socket = null;
OutputStream out;
try {
socket = device.createRfcommSocketToServiceRecord(SERIAL_UU ID);
} catch (IOException e) {}


try {
socket.connect();
out = socket.getOutputStream();
//now you can use out to send output via out.write
byte[] msgBuffer = "hello".getBytes(); // اینجا تبدیل به بایت میشه و ارسال میشه
out.write(msgBuffer);
} catch (IOException e) {}

a1malek
پنج شنبه 19 آذر 1394, 19:46 عصر
این کد رو هم ببینید


using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using System.Net.Sockets;
using System.Diagnostics;
using System.Threading;


namespace dConsoleApp
{
static class Program
{
// My BT USB adapter
private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("AC:72:89:CF:F9:E9"), BluetoothService.BluetoothBase);
private static BluetoothClient BC = new BluetoothClient(EP);

// The BT device that would connect
private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("20:54:76:7C:DA:97"));

private static NetworkStream stream = null;
private static string pin = Console.ReadLine();
static void Main(string[] args)
{
if (BluetoothSecurity.PairRequest(BTDevice.DeviceAddr ess,pin))
{
Console.WriteLine("PairRequest: OK");

if (BTDevice.Authenticated)
{
Console.WriteLine("Authenticated: OK");

BC.SetPin(pin);

BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
}
else
{
Console.WriteLine("Authenticated: No");
}
}
else
{
Console.WriteLine("PairRequest: No");
}

Console.ReadLine();
}

private static void Connect(IAsyncResult result)
{
Console.WriteLine("here");
if (result.IsCompleted)
{
// client is connected now :)
Console.WriteLine(BC.Connected);

stream = BC.GetStream();

if (stream.CanRead)
{
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;

// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);

myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while (stream.DataAvailable);

// Print out the received message to the console.
Console.WriteLine("You received the following message : " + myCompleteMessage);
}
else
{
Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
}
Console.ReadLine();
}
else
Console.WriteLine("Sorry. You cannot read from this kkkkNetworkStream.");
}
}
}


از نظر تءوری درسته ولی برای من pair میشه و Authenticated هم ok میشه اما زمان کانکت شدن ارور میده هیچ سر نخی هم پیدا نمیکنم کی پیش رو بگیرم و ببینم مشکل از کجاست فقط میدونم به موبایل من کانکت نمیشه


BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);


اینجا و

stream = BC.GetStream();
اینجا

137383137384

a1malek
پنج شنبه 19 آذر 1394, 21:43 عصر
جواب رو پیدا کردم
پروتکل رو گذاشتم روی RFCommProtocol و کانکت شد ولی نتونستم رشته رو نشون بدم