hello I connect my device android to printers, of the way follows.
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetakebt220 =
bluetoothAdapter.getRemoteDevice(obj.getMac().toString());
BluetoothSocket mBTsocket= null;
bluetoothAdapter.cancelDiscovery();
UUID num = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
mBTsocket = bluetakebt220.createRfcommSocketToServiceRecord(num);
mBTsocket.connect();
this works well for for most devices, but some do not connected, ¿that can be due?
The most obvious reason I can think of is the UUID's do not match up. If you are making a generic app you need to have the UUID of each printer. This can be done in the form of a case switch statement.
Code
switch(Mac Address) {
case (Mac Address 1)
UUID = xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
case (Mac Address 2):
UUID = xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
default:
UUID = 00001101-0000-1000-8000-00805F9B34FB;
The reason why most of the time your connections works is because 00001101-0000-1000-8000-00805F9B34FB is the generic ID for most off shelf hardware devices (keyboards,mouses,printers,scanners etc). The minute your UUID does not match a connection cannot occur. Read this for more info about UUID's.
the device acts equal, simply does not establish proper connection , but yes allow write in
Here is the code if someone can help
UUID num = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetakebt220 = bluetoothAdapter.getRemoteDevice(obj.getMac().toString());//PRINTER ADAPTER FOR LPT BLUETAKE BT220
BluetoothSocket mBTsocket= null;
mBTsocket = bluetakebt220.createRfcommSocketToServiceRecord(num);
bluetoothAdapter.cancelDiscovery();
mBTsocket.connect();
OutputStream os = **mBTsocket**.getOutputStream();
os.flush();
byte[] CPCLFormat = null;
PCLFormat = objImpresion.getTexto().getBytes("utf-8");
os.write(CPCLFormat);//se imprime el texto
os.flush();
os.close();
.I simply comment msocket.isConnected and write directly after open connection. i don´t why happen
Related
I have this project, where I need to create a BLE-Server (ESP32) to exchange data with a Laptop/Smartphone etc.... . The code is written with Arduino IDE, using the ESP32 BLE Arduino library and seems like the following
void Setup() {
BLEDevice::init("MIDI_BOX"); // Name of the BLE_Device
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
MIDI_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
};
Having this said, when I want to connect my Laptop or Smartphone to this server. It needs to indetify which kind of device is connected to(Apple, Microsoft,). The manufacturer data seems a good idea for me but I don't how to get this information when there is a connection.
A callback fonction is provided by the BLEServer:
class MyServerCallbacks: public BLEServerCallbacks { //can I here do smth to get manufacturer data ??
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
Can anyone explain to me how it should be done, or are there any better way of thinking ?
Thnx in advance
I'm using tap device.
The problem is that you just can ->read() or ->write() one packet every system call.
After read the source file tun.c, I found there is struct socket in tun device and function tun_get_socket() can return it.
struct socket *tun_get_socket(struct file *file)
{
struct tun_file *tfile;
if (file->f_op != &tun_fops)
return ERR_PTR(-EINVAL);
tfile = file->private_data;
if (!tfile)
return ERR_PTR(-EBADFD);
return &tfile->socket;
}
EXPORT_SYMBOL_GPL(tun_get_socket);
The socket.ops is set with tun_socket_ops which have ->sendmsg() and ->recvmsg().
static const struct proto_ops tun_socket_ops = {
.sendmsg = tun_sendmsg,
.recvmsg = tun_recvmsg,
.release = tun_release,
};
The questions:
How to create one file descriptor to connect with this socket? That makes me to use ->sendmsg() and ->recvmsg() in userspace. My OS is CentOS7.9 with kernel version 3.10.0-1160.el7.x86_64.
Or is there other ways I can read or write multiple packets at one time from tap device?
Writing new kernel module is acceptable.
Hi I am writing a simple console app that needs to write bytes to MCP2210 USB to SPI Master
I found this library over here, seems to do good job with connecting the device and reading the metadata.
I am writing message to the board as below
public static byte[] Talk()
{
var device = DeviceList.Local.GetHidDevices(1240, 222).FirstOrDefault();
if (device == null)
{
Console.WriteLine($"Could not find a device with Vendor Id:1240, Product Id:222 ");
return null;
}
var reportDescriptor = device.GetReportDescriptor();
foreach (var deviceItem in reportDescriptor.DeviceItems)
{
Console.WriteLine("Opening device for 20 seconds...");
if (!device.TryOpen(out var hidStream))
{
Console.WriteLine("Failed to open device.");
continue;
}
Console.WriteLine("Opened device.");
hidStream.ReadTimeout = Timeout.Infinite;
hidStream.Write(new byte[3] {60, 00, 00});
}
Not sure If I am writing it correctly.
While writing I need to do a chip selection as displayed in this other terminal
Any help is greatly appreciated
Here is the MC I am using https://www.microchip.com/wwwproducts/en/MCP2210
I do not see a closing of your stream. This may cause your data to not even being sent (at least not in time).
Consider using blocks with streams.
But with out parameters not possible.
I prepared an iphone application that gets push notification messages. I can send push message using with 2195 port , gateway.sandbox.push.apple.com and local p12 file
My application in appstore, now i'm using gateway.push.apple.com and distribution p12 file, but messages cannot reach to real application (in appstore)
Device ids are different in local and prod version, I know this so i'm trying to send new device id from .net
Same codes works in local and i can send messages but when i send push message to real application, no message reach to iphone with the same codes. I have no error during this period.
What can i do?
my c# code like below. How can i solve my problem? There is no error but message cannot reach to devices with distribution cert.
int port = 2195;
String hostname = "gateway.push.apple.com";
String certificatePath = HttpContext.Current.Server.MapPath("distribution.p12");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "xxx", X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
}
catch (Exception e)
{
throw (e);
client.Close();
return;
}
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);
writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
//String deviceID = "f1430c99 910d292d 2f756294 f2f6b348 153bc215 d5404447 16b294eb fdb9496c";
writer.Write(HexStringToByteArray(deviceID.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"" + Mesaj + "\",\"badge\":0,\"sound\":\"default\"}}";
writer.Write((byte)0);
writer.Write((byte)payload.Length);
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
client.Close();
After enabled your push notification in your profile appid.
Then only you can able to get device token from the running device. Based on the device token you may send notification.
development process : gateway.sandbox.push.apple.com port 2195.
distribution process : gateway.push.apple.com port 2195.
refer from this links: https://stackoverflow.com/a/10045210/510814
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
I did try to get internet IP by using GetIp method (below) but it only get one IP that's 123.22.114.5 , I restarted my modem many times but it still 123.22.114.5, please help me. (I'm using visual studio 2010 in windows 7 64bit)
(sorry about my bad english)
private string GetIp()
{
string extenalIp = "";
string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
string getIpRegex = #"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (Exception e)
{
// do some thing with exception
Console.WriteLine(e.Message);
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
if (m.Success)
{
extenalIp = m.Value.ToString();
}
wc.Dispose();
return extenalIp;
}
You would get just one IP which is your ISP's external IP address, when you request some external website like whatismyip.
All ISPs have limited static IP addresses and hosts facing the internet.
That WAN IP address is shared among many users usually.
The IP you see there is completely different from your local IP address, and hence, restarting your modem won't change it.