Search code examples
iosbluetoothgoogle-glass

Transfer data between iPhone and Google Glass


I am looking to connect my phone to my Google Glass and transfer data (photos, text, etc.) between the two.

I was thinking of using Bluetooth LTE but as I understand it, Glass does not support it because it is only running version 4.0.3 (or similar) of Android.

I know you can connect the phone and glass via the myGlass app, so clearly, what I am looking to do is possible. However, I was wondering if someone could point me in the right direction to get started? Specifically, what technologies should I look at if not CoreBluetooth on the iOS side? Has anyone else done this?

Also, if it would be better served to use Bonjour, or even to create the hotspot on my iPhone and connect that way, any tutorials you could point me to would be great.

Thanks in advance.


Solution

  • I'm writing a Google Glass Development book for Apress and just finished the chapter Network and Bluetooth, with some working samples to let Glass communicate with iPhone for data transfer. You're right that Glass as of now (API level 15) doesn't support Bluetooth Low Energy (BLE). I have implemented three ways to make the data transfer between Glass and iOS happen:

    1. Let Glass talk to an Android device, such as Nexus 7 with Android 4.3 or above with BLE support, via Classic Bluetooth or socket, and Nexus 7 acts as a BLE central to talk to iOS as a BLE peripheral. Notice you shouldn't use BLE to send large data such as photo.

    2. Let Glass talk to iOS directly via socket - you can use C socket code running as a server and Glass Java socket client, or vice versa. This would require your Glass and iOS device on the same Wifi, but can transfer large data.

    3. Use a server-based solution - upload data from Glass to a server and let iOS get it via Apple Push Notification. I used this method to share photos on Glass with friends on WhatsApp and WeChat, both apps run on iOS.

    Sample iOS code acting as socket server:

    - (void) runSocketServer {
        int listenfd = 0;
        __block int connfd = 0;
        struct sockaddr_in serv_addr;
    
        __block char sendBuff[1025];
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        memset(&serv_addr, '0', sizeof(serv_addr));
        memset(sendBuff, '0', sizeof(sendBuff));
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(6682);
    
        bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));    
        listen(listenfd, 10);
    
        dispatch_async(dispatch_get_global_queue(0, 0), ^{        
            connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
            int count = 1;
            while (count++ < 120) {
                char rate[100];
                sprintf(rate, "%i\n", bpm);            
                write(connfd, rate, strlen(rate));            
                sleep(1);
            }
    
            close(connfd);
        });
    }
    

    Sample Glass code acting as a socket client:

    public void run()
    {
        String serverName = "192.168.1.11"; 
        int port = 6682;
        try
        {
            socket = new Socket(serverName, port);
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            do {
                result = input.readLine();
                runOnUiThread(new Runnable() {
                    public void run() {
                        mTvInfo.setText(result);
                    }
                });                 
            } while (result != null);
    
            });                  
        }
        catch(Exception e) {
            try { socket.close(); }
            catch (Exception e2) {};
            e.printStackTrace();
        }
    }