summaryrefslogtreecommitdiff
path: root/library/ADK2/examples/BluetoothScan/BluetoothScan.pde
blob: f950aaf3ec31dad8f42fe3b71f037372dc519ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <ADK.h>
#include <HCI.h>


ADK L;

extern "C"{
 void dbgPrintf(const char* fmt,...);  //arduino print sucks... 
 void* malloc(unsigned sz);  //who the hell programs BT without these?
 void free(void*);  //who the hell programs BT without these?
}



void adkPutchar(char c){
 Serial.write(c);
}

void setup(void)
{

 Serial.begin(115200);
 
 L.adkSetPutchar(adkPutchar);
 L.adkInit();
}

typedef struct BTD{

    struct BTD* next;
    uint8_t mac[BLUETOOTH_MAC_SIZE];
    uint8_t PSRM;
    uint8_t PSPM;
    uint8_t PSM;
    uint16_t CO;
    uint32_t dc;

}BTD;

BTD* devs;

char adkBtDiscoveryResult(const uint8_t* mac, uint8_t PSRM, uint8_t PSPM, uint8_t PSM, uint16_t CO, uint32_t devClass){ //return 0 to stop scan immediately

    int i;
    static int nres = 0;

    dbgPrintf("%d results so far...\n", ++nres);

    BTD* btd = devs;
    while(btd){

        if(btd->mac[0] == mac[0] && btd->mac[1] == mac[1] && btd->mac[2] == mac[2] && 
		btd->mac[3] == mac[3] && btd->mac[4] == mac[4] && btd->mac[5] == mac[5]) return 1;
        btd = btd->next;
    }

    btd = (BTD*)malloc(sizeof(BTD));
    if(!btd) return 0;

    for(i = 0; i < BLUETOOTH_MAC_SIZE; i++) btd->mac[i] = mac[i];
    btd->PSRM = PSRM;
    btd->PSPM = PSPM;
    btd->PSM = PSM;
    btd->CO = CO;
    btd->dc = devClass;
    btd->next = devs;
    devs = btd;
    return 1;
}

void doScan(){

    devs = NULL;

    dbgPrintf("BT Scan...\n");
    L.btScan();
    dbgPrintf("\n");

    BTD* dev = devs;

    while(dev){

        dbgPrintf("%02X:%02X:%02X:%02X:%02X:%02X - ", dev->mac[5], dev->mac[4], dev->mac[3], dev->mac[2], dev->mac[1], dev->mac[0]);
        dbgPrintf("{%d,%d,%d} 0x%06X %6d - ", dev->PSRM, dev->PSPM, dev->PSM, dev->dc, dev->CO);

        switch((dev->dc & DEVICE_CLASS_MAJOR_MASK) >> DEVICE_CLASS_MAJOR_SHIFT){

            case DEVICE_CLASS_MAJOR_MISC:
                dbgPrintf("MISC");
                break;

            case DEVICE_CLASS_MAJOR_COMPUTER:
                dbgPrintf("Computer - ");

                switch((dev->dc & DEVICE_CLASS_MINOR_COMPUTER_MASK) >> DEVICE_CLASS_MINOR_COMPUTER_SHIFT){

                    case DEVICE_CLASS_MINOR_COMPUTER_UNCATEG:
                        dbgPrintf("uncategorized");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_DESKTOP:
                        dbgPrintf("desktop");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_SERVER:
                        dbgPrintf("server");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_LAPTOP:
                        dbgPrintf("laptop");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_CLAM_PDA:
                        dbgPrintf("clamsghell PDA");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_PALM_PDA:
                        dbgPrintf("Palm PDA");
                        break;
                    case DEVICE_CLASS_MINOR_COMPUTER_WEARABLE:
                        dbgPrintf("wearable");
                        break;
                    default:
                        dbgPrintf("unknown minor %d", (dev->dc & DEVICE_CLASS_MINOR_COMPUTER_MASK) >> DEVICE_CLASS_MINOR_COMPUTER_SHIFT);
                        break;
                }
                break;

            case DEVICE_CLASS_MAJOR_PHONE:
                dbgPrintf("Phone - ");
                switch((dev->dc & DEVICE_CLASS_MINOR_PHONE_MASK) >> DEVICE_CLASS_MINOR_PHONE_SHIFT){

                    case DEVICE_CLASS_MINOR_PHONE_UNCATEG:
                        dbgPrintf("uncategorized");
                        break;
                    case DEVICE_CLASS_MINOR_PHONE_CELL:
                        dbgPrintf("Cellular");
                        break;
                    case DEVICE_CLASS_MINOR_PHONE_CORDLESS:
                        dbgPrintf("Cordless");
                        break;
                    case DEVICE_CLASS_MINOR_PHONE_SMART:
                        dbgPrintf("Smartphone");
                        break;
                    case DEVICE_CLASS_MINOR_PHONE_MODEM:
                        dbgPrintf("Modem");
                        break;
                    case DEVICE_CLASS_MINOR_PHONE_ISDN:
                        dbgPrintf("ISDN");
                        break;
                    default:
                        dbgPrintf("unknown minor %d", (dev->dc & DEVICE_CLASS_MINOR_PHONE_MASK) >> DEVICE_CLASS_MINOR_PHONE_SHIFT);
                        break;
                }
                break;

            case DEVICE_CLASS_MAJOR_LAN:
                dbgPrintf("LAN");
                break;

            case DEVICE_CLASS_MAJOR_AV:
                dbgPrintf("A/V - ");
                switch((dev->dc & DEVICE_CLASS_MINOR_AV_MASK) >> DEVICE_CLASS_MINOR_AV_SHIFT){

                    case DEVICE_CLASS_MINOR_AV_UNCATEG:
                        dbgPrintf("uncategorized");
                        break;
                    case DEVICE_CLASS_MINOR_AV_HEADSET:
                        dbgPrintf("Headset");
                        break;
                    case DEVICE_CLASS_MINOR_AV_HANDSFREE:
                        dbgPrintf("Handsfree");
                        break;
                    case DEVICE_CLASS_MINOR_AV_MIC:
                        dbgPrintf("Microphone");
                        break;
                    case DEVICE_CLASS_MINOR_AV_LOUDSPEAKER:
                        dbgPrintf("Loudspeaker");
                        break;
                    case DEVICE_CLASS_MINOR_AV_HEADPHONES:
                        dbgPrintf("Headphones");
                        break;
                    case DEVICE_CLASS_MINOR_AV_PORTBL_AUDIO:
                        dbgPrintf("Portable Audio");
                        break;
                    case DEVICE_CLASS_MINOR_AV_CAR_AUDIO:
                        dbgPrintf("Car audio");
                        break;
                    case DEVICE_CLASS_MINOR_AV_SET_TOP_BOX:
                        dbgPrintf("Set-Top Box");
                        break;
                    case DEVICE_CLASS_MINOR_AV_HIFI:
                        dbgPrintf("Hi-Fi");
                        break;
                    case DEVICE_CLASS_MINOR_AV_VCR:
                        dbgPrintf("VCR");
                        break;
                    case DEVICE_CLASS_MINOR_AV_VID_CAM:
                        dbgPrintf("Vidoe Camera");
                        break;
                    case DEVICE_CLASS_MINOR_AV_CAMCORDER:
                        dbgPrintf("Camcorder");
                        break;
                    case DEVICE_CLASS_MINOR_AV_VID_MONITOR:
                        dbgPrintf("Video Monitor");
                        break;
                    case DEVICE_CLASS_MINOR_AV_DISPLAY_AND_SPKR:
                        dbgPrintf("Display With Speakers");
                        break;
                    case DEVICE_CLASS_MINOR_AV_VC:
                        dbgPrintf("Video conferencing device");
                        break;
                    case DEVICE_CLASS_MINOR_AV_TOY:
                        dbgPrintf("Toy");
                        break;
                    default:
                        dbgPrintf("unknown minor %d", (dev->dc & DEVICE_CLASS_MINOR_AV_MASK) >> DEVICE_CLASS_MINOR_AV_SHIFT);
                        break;
                }
                break;

            case DEVICE_CLASS_MAJOR_IMAGING:
                dbgPrintf("Imaging");
                break;

            case DEVICE_CLASS_MAJOR_WEARABLE:
                dbgPrintf("Wearable");
                break;

            case DEVICE_CLASS_MAJOR_TOY:
                dbgPrintf("Toy");
                break;

            case DEVICE_CLASS_MAJOR_HEALTH:
                dbgPrintf("Health");
                break;

            case DEVICE_CLASS_MAJOR_UNCATEGORIZED:
                dbgPrintf("Uncategorize");
                break;

            default:
                dbgPrintf("UNKNOWN_MAJOR_%d", (dev->dc & DEVICE_CLASS_MAJOR_MASK) >> DEVICE_CLASS_MAJOR_SHIFT);
                break;
        }

        char name[249];
        if(L.btGetRemoteName(dev->mac, dev->PSRM, dev->PSM, dev->CO, name)) dbgPrintf(" \"%s\"", name);
        else dbgPrintf("{FAILED TO GET DEVICE NAME}");

        dbgPrintf("\n");

        devs = dev;
        dev = dev->next;
        free(devs);
    }
    dbgPrintf("Scan done\n");
}

void loop(void)
{
 
 L.btEnable(0, 0, 0, 0, adkBtDiscoveryResult);
 
 while(1) {
   
     L.adkEventProcess();
     doScan();
 }
}