As far as I know there is no way to automatically connect to a generic Bluetooth device as soon as it's in range. Your best bet is to have an inquiry loop, periodically querying available devices and connecting to ones you are bonded with (although, as you said, this is fairly battery intensive).
However, if your device (both the phone and the peripheral) support Bluetooth 4.0 (LE) as well as GATT, you can use connectGatt method that will automatically connect as soon as the device becomes available (again, not too sure about battery implications although limited testing I have done thus far shows that it is not significant).
I'm not entirely certain what the behaviour of connectGatt
is when device is non-GATT but it may be worth investigating - my guess would be that you'd receive a callback with status GATT_FAILURE
when device becomes in range but doesn't support GATT (this would at least give you an indication of when to connect).
If you find a solution or investigate the behaviour of this method on non-LE / non-GATT devices please do update your question as I'd be quite curious to find out more about it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…