Agamnentzar / bluetooth-serial-port

multi-platform bluetooth serial port library for C++
Other
89 stars 27 forks source link

Windows use of library #3

Closed ameeraal1 closed 8 years ago

ameeraal1 commented 8 years ago

I am attempting to use this library in my project, how do I include it in my c++ project? What I've tried: Built the project using CMake - specifying the downloaded unzipped project as the source and another folder for the binary output.

My build output: http://i65.tinypic.com/2vmfu4o.png

I have built a .lib file and added it to Linker -> Input -> Additional Dependencies. Then I included the .lib file path directory, however when I try to #include "DeviceINQ.h" or #include I get an error can't find source file. I tried to view the contents of the .lib file in Visual Studio Command Prompt, this was the output:

http://i64.tinypic.com/1zvrivb.png

Here it shows that DeviceINQ is an object but I still cannot include the header file. Any Ideas, thankyou.

Agamnentzar commented 8 years ago

Did you add bluebooth-serial-port/src to include paths for the project ? (Configuration Properties -> VC++ Directories -> Include Directories)

ameeraal1 commented 8 years ago

1>c:\libraries\bluetooth-serial-port-master\bluetooth-serial-port-master\src\enums.cc(199): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? I added the directory but got this error

Agamnentzar commented 8 years ago

Is it during building your project or bluetooth-serial-port ? It should not compile enums.cc when compiling your project, not sure why it would do so. You can try disabling precompiled headers and see if it helps.

ameeraal1 commented 8 years ago

Building my project. I removed the compiled headers and built the project and it successfully built. However when I try to build with your example I get:

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol __imp__WSAAddressToStringA@20 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol __imp__WSALookupServiceBeginA@12 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol __imp__WSALookupServiceNextA@16 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol __imp__WSALookupServiceEnd@4 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(DeviceINQ.obj) : error LNK2019: unresolved external symbol _BluetoothGetDeviceInfo@8 referenced in function "public: class std::vector<struct device,class std::allocator<struct device> > __thiscall DeviceINQ::Inquire(void)" (?Inquire@DeviceINQ@@QAE?AV?$vector@Udevice@@V?$allocator@Udevice@@@std@@@std@@XZ)

1>bluetoothserialport.lib(BluetoothHelpers.obj) : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: static bool __cdecl BluetoothHelpers::Initialize(void)" (?Initialize@BluetoothHelpers@@SA_NXZ
Agamnentzar commented 8 years ago

add ws2_32.lib and bthprops.lib to libraries

ameeraal1 commented 8 years ago

Thank you the above additions they worked, and the bluetooth devices are shown. However, when I try to return the channel id with inq->SdpSearch(d.address) I am getting an invalid argument error (10022). Do you have any more example code for using the features of this library?

When omit sdpsearch and instead use : unique_ptr<BTSerialPortBinding> bt(BTSerialPortBinding::Create(d1.address, 38)); Hard coding the com port number throws no errors, however Sdpsearch throws an exception when I try to search for the port number using this method.


int main()
{
    try
    {
        unique_ptr<DeviceINQ> inq(DeviceINQ::Create());
        vector<device> devices = inq->Inquire();
        device d1;

        for (const auto& d : devices)
        {
            cout << "\tname: " << d.name << endl;
            cout << "\taddress: " << d.address << endl;
            cout << "\tclass: " << GetDeviceClassString(d.deviceClass) << endl;
            cout << "\tmajor class: " << GetDeviceClassString(d.majorDeviceClass) << endl;
            cout << "\tservice class: " << GetServiceClassString(d.serviceClass) << endl;
            cout << "\tlast seen: " << formatDate("%c", d.lastSeen) << endl;
            cout << "\tlast used: " << formatDate("%c", d.lastUsed) << endl;
                        // problem statement
            cout << "\ChannelID: " << inq->SdpSearch(d.address) << endl;
            cout << endl;
        }
    }  
    catch (const BluetoothException& e)
    {
        cout << "here" << endl;
        cout << e.what() << endl;
    }

    return 0;
}
Agamnentzar commented 8 years ago

Sorry, wasn't testing that method, fixed it just now in 8fc5e16b968ca0f80bec447d4aadef2d988993bd

ameeraal1 commented 8 years ago

Thankyou! I will try this now, but with my method using hardcoded com port number, I get the error Cannot connect: (10049) The request address is not valid in its context when I try to do bt->connect. I will try with the new commit now and see if it can connect with the comm returned from sdp search.

Agamnentzar commented 8 years ago

For me passing 1 for channel id worked just fine in all cases, might depend on the device though.

ameeraal1 commented 8 years ago

Thankyou, just passed 1 and it worked well. For reference what does SdpSearch actually return? I thought it returned the com port in which the bluetooth device is using. Also what is the method isDataAvailable() checking?

Agamnentzar commented 8 years ago

It does return channel id, it's always 1 for me, might be different for devices that have many bluetooth services. IsDataAvailable() checks if there is data in the buffer, helps for doing non-blocking checks. I had some specific code that needed this check.

ameeraal1 commented 8 years ago

Great thank you. I am now trying to read the data coming through, which works well. However when I try return the connection status d1.connected returns false even though I can read data coming through from my HC-05.

 unique_ptr<BTSerialPortBinding> bt(BTSerialPortBinding::Create(d1.address, 1));
 bt->Connect();
cout << d1.connected << endl; //returns 0;

char incomingData[256] = "";                   // don't forget to pre-allocate memory
                                                //printf("%s\n",incomingData);
int dataLength = 255;
int readResult = 0;

// this outputs the incoming data from the bluetooth module
while (1)
{
    readResult = bt->Read(incomingData, dataLength);
    incomingData[readResult] = 0;

    printf("%s", incomingData);

        Sleep(500);
}
Agamnentzar commented 8 years ago

connected field actually tells if device is connected to the PC at the time of querying, it won't change after that

ameeraal1 commented 8 years ago

Is there a way to check if a device is still connected to the PC while reading data?

Agamnentzar commented 8 years ago

The Read() will throw or return 0 if the connection is lost.

ameeraal1 commented 8 years ago

If I want to open a file as a standard I/O stream and read from it where would I get to comm port from in the library?

        FILE *fp = fdopen(comm, "r+");
        if (fscanf(fp, "6,%d,%d,%d\r\n", &x, &y, &z) == 3) {
            printf("x:%d,y:%d,z:%d\n", x, y, z);
        }
Agamnentzar commented 8 years ago

It's not going to be available as a comport to the system. You'll have to use Read and Write methods from BTSerialPortBinding. Don't know if there is a way to provide bluetooth device access to be visible as a regular system comport.